mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-23 05:00:06 +01:00
feat: Schema can be de/serialized from a json format
This commit is contained in:
parent
cdc6e47bf5
commit
fa062ce2cf
@ -17,6 +17,7 @@ log = "0.4"
|
|||||||
sdset = "0.3"
|
sdset = "0.3"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||||
unidecode = "0.3"
|
unidecode = "0.3"
|
||||||
|
|
||||||
[dependencies.toml]
|
[dependencies.toml]
|
||||||
|
@ -113,6 +113,23 @@ impl Schema {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_json<R: Read>(mut reader: R) -> Result<Schema, Box<Error>> {
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
reader.read_to_end(&mut buffer)?;
|
||||||
|
let builder: SchemaBuilder = serde_json::from_slice(&buffer)?;
|
||||||
|
Ok(builder.build())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_json<W: Write>(&self, mut writer: W) -> Result<(), Box<Error>> {
|
||||||
|
let identifier = self.inner.identifier.clone();
|
||||||
|
let attributes = self.attributes_ordered();
|
||||||
|
let builder = SchemaBuilder { identifier, attributes };
|
||||||
|
let string = serde_json::to_string_pretty(&builder)?;
|
||||||
|
writer.write_all(string.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn read_from_bin<R: Read>(reader: R) -> bincode::Result<Schema> {
|
pub(crate) fn read_from_bin<R: Read>(reader: R) -> bincode::Result<Schema> {
|
||||||
let builder: SchemaBuilder = bincode::deserialize_from(reader)?;
|
let builder: SchemaBuilder = bincode::deserialize_from(reader)?;
|
||||||
Ok(builder.build())
|
Ok(builder.build())
|
||||||
@ -254,4 +271,40 @@ mod tests {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_deserialize_json() -> Result<(), Box<Error>> {
|
||||||
|
let mut builder = SchemaBuilder::with_identifier("id");
|
||||||
|
builder.new_attribute("alpha", STORED);
|
||||||
|
builder.new_attribute("beta", STORED | INDEXED);
|
||||||
|
builder.new_attribute("gamma", INDEXED);
|
||||||
|
let schema = builder.build();
|
||||||
|
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
schema.to_json(&mut buffer)?;
|
||||||
|
|
||||||
|
let schema2 = Schema::from_json(buffer.as_slice())?;
|
||||||
|
assert_eq!(schema, schema2);
|
||||||
|
|
||||||
|
let data = r#"
|
||||||
|
{
|
||||||
|
"identifier": "id",
|
||||||
|
"attributes": {
|
||||||
|
"alpha": {
|
||||||
|
"stored": true
|
||||||
|
},
|
||||||
|
"beta": {
|
||||||
|
"stored": true,
|
||||||
|
"indexed": true
|
||||||
|
},
|
||||||
|
"gamma": {
|
||||||
|
"indexed": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}"#;
|
||||||
|
let schema2 = Schema::from_json(data.as_bytes())?;
|
||||||
|
assert_eq!(schema, schema2);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user