add AsRef, expose object and array iter on Value (#2207)

add AsRef
expose object and array iter
add to_json on Document
This commit is contained in:
PSeitz
2023-10-05 03:55:35 +02:00
committed by GitHub
parent 5b1bf1a993
commit b700c42246
6 changed files with 43 additions and 13 deletions

View File

@@ -195,14 +195,6 @@ impl TantivyDocument {
Ok(document)
}
/// Encode the schema in JSON.
///
/// Encoding a document cannot fail.
pub fn to_json(&self, schema: &Schema) -> String {
serde_json::to_string(&self.to_named_doc(schema))
.expect("doc encoding failed. This is a bug")
}
/// Build a document object from a json-object.
pub fn parse_json(schema: &Schema, doc_json: &str) -> Result<TantivyDocument, DocParsingError> {
let json_obj: Map<String, serde_json::Value> =

View File

@@ -235,6 +235,14 @@ pub trait Document: DocumentDeserialize + Send + Sync + 'static {
}
NamedFieldDocument(field_map)
}
/// Encode the doc in JSON.
///
/// Encoding a document cannot fail.
fn to_json(&self, schema: &Schema) -> String {
serde_json::to_string(&self.to_named_doc(schema))
.expect("doc encoding failed. This is a bug")
}
}
/// A single field value.
@@ -357,6 +365,26 @@ pub trait Value<'a>: Send + Sync + Debug {
}
}
#[inline]
/// Returns the iterator over the array if the Value is an array.
fn as_array(&self) -> Option<Self::ArrayIter> {
if let ReferenceValue::Array(val) = self.as_value() {
Some(val)
} else {
None
}
}
#[inline]
/// Returns the iterator over the object if the Value is an object.
fn as_object(&self) -> Option<Self::ObjectIter> {
if let ReferenceValue::Object(val) = self.as_value() {
Some(val)
} else {
None
}
}
#[inline]
/// Returns true if the Value is an array.
fn is_array(&self) -> bool {