Basically what you need is JSON in class form. JSON can only contain certain types like number, string, object, array. So the document data must consist exclusively of these classes (with the exception of the Blob class). Any custom types you have must be semi-serialized into such so if you had something like:
class MyThing {
string name;
int age;
}
Then you should pass in a dictionary containing the keys name
and age
with the corresponding values (as you would find them in the JSON string).
// Forgive my syntax, I am not a Java/Kotlin dev, so this is pseudo
val map = {
"name": "Fred",
"age": 24
};
This goes back to what I said about people more often requesting ways to pass in their own data types directly to the library.
One common paradigm that people follow to comply with this is that their classes will all implement an interface that has a method to convert them to dictionaries (similar to the Apple platform NSSerialization
class or .NET ISerializable
) and to convert them to a dictionary they call the conversion method on their class, which would then recursively go through its data and add in the dictionaries representing its member object.
// Simple pseudo example
interface DocumentSerializable {
Map<String, Object> toDocumentData();
}
class MyNestedThing implements DocumentSerializable {
string id;
MyNestedThing (Map<String, Object> documentData) {
id= documentData.get("id");
}
Map<String, Object> toDocumentData() {
return {
"id": id
}
}
}
class MyThing implements DocumentSerializable {
String name;
int age;
MyNestedThing nestedThing;
MyThing(Map<String, Object> documentData) {
name = documentData.get("name");
age = documentData.get("age");
nestedThing = new MyNestedThing(documentData.get("nestedThing"));
}
Map<String, Object> toDocumentData() {
return {
"name": name,
"age": age,
"nestedThing": nestedThing.toDocumentData()
}
}
}
Then if you have a MyThing
instance you just need to make a new MutableDocument
(or modify an existing one) with it:
new MutableDocument("my-chosen-id", myThing.toDocumentData())
existingMutableDoc.setData(myThing.toDocumentData())
This is coming from the other angle than I imagined based on the previous posts, which was you have a JSON string in which case you simply do this:
Map<String, Object> myData = deserialize(myJsonString);
MutableDocument myDoc = new MutableDocument("my-chosen-id", myData);
The second example is the one I am envisioning as not very complicated. The first, which is a separate problem, is more involved.