Sdk v2 storing docs


I am new to v2, and trying to store document using JsonStringDocument and realized that it is stored base64 encoded, and currently view cannot work with that. Should I work with StringDocument or am I missing something?


Hey Oded,

If what you want to do is store some JSON that you already have obtained as a String (from an external library like Jackson or GSON), then you can use the RawJsonDocument.
JsonStringDocument, JsonArrayDocument, etc… where meant for scalar values, ie store a single item rather than a full JSON object, which is not completely equivalent to what you are trying to do (thus the RawJsonDocument was introduced in a following version).

Of course, if you’re not using any third-party JSON libraries, the best supported way is to build JsonObject (using its create methods) rather than plain Strings and work with the JsonDocument.

Happy coding

1 Like

But does the document kept in CB as base64 encoded string? Another question: When should I work with JsonDocument and with RawJsonDocument or other?

Both RawJsonDocument and JsonDocument store data in a way views can work with on the server side (that is, a JSON object).

You’d use mostly JsonDocument if you only deal with domain objects and not JSON in your code:

Domain Object -> JsonObject -> JsonDocument -> Database.

That is, except if you already transform your data into JSON with a String representation (for example by using a JSON library like Jackson or GSON), because in this case using the JsonDocument adds an unnecessary extra step :

not optimized: Object-oriented data -> Json String obtained eg via Jackson -> extra parsing and conversion to JsonObject -> JsonDocument -> Database

better: Domain Object -> JSON String (using Jackson, …) -> RawJsonDocument -> Database

I hope this is clearer