Storing List<Map<Enum,Object>> datatype as JSON in DynamoDb - amazon-dynamodb

In Java, when using DynamoDbMapper, what is the best way to store List<Map<Enum,Object>> datatype as JSON? Which annotations should we use to achieve this? In our case, the Enum could be serialized into a simple string, while the Object could be serialized into JSON string.
Also, could anyone clarify in which situation one must use each of these annotations #DynamoDBTypeConverter, #DynamoDBTypeConvertedJson, and #DynamoDbDocument?

Related

What's the difference between serialize and encode?

I'm using Symfony\Component\Serializer\Serializer, and I'm not sure what the difference between the serialize and encode methods is. They have identical signatures, and in practice they seem to give identical output.
Based on the official docs, serializing involves two phases: normalizing and encoding. Normalizing converts input data into array, while encoding crunches that array down to desirable format (be it JSON, XML or something else).
The serialize method of the Symfony Serializer is a wrapper on its encode method. Note that you can call the encode method separately. The serialize method can call the normalize method before the encode method depending on the fact that the requested encoder (eg: json) needs normalization or not.
If you intend to do a JSON serialization, the encode method of the serializer will eventually call json_encode PHP native method. And this method performs a serialization in fact...
For example, if you look into the jsonSerialize method of the jsonSerializable native class of PHP, you can read in the description:
Serializes the object to a value that can be serialized natively by json_encode().
So, at least, in the case of the JSON format, we can say that encoding is serializing in fact but in lower level.
If you call the encode method directly without using the serialize method, you will serialize your data into the expected format but will not take benefits of the normalize process if needed.

Get the results of a CosmosDb query as a Raw string (payload of the http response)

I'm using the .NET API of CosmosDB and I'm getting a hard time trying to figure out how to get the raw result of a CosmosDB query prior to it getting deserialized into a class. Browsing the documentation, all examples I find cast the results to an specific class or to a dynamic. That is:
//This returns a Document, wich actually is a dynamic...
client.ReadDocumentAsync(...)
//This returns an object of type MyClass, wich I supose is casted internally by the API
client.ReadDocumentAsync<MyClass>(...)
What I want to do is to get the original raw JSON payload of the result to inspect it without the overhead of deserializing it to anything else.
Does anybody know if it's possible to get the raw result with the .NET api? If so, how?
In other cases, I need to use the result as an ExpandoObject to treat it dynamically, but I find that the "dynamic" results given by the api are not "expandables" so I'm forced to serialize them and then deserialize again in a recursive form into an ExpandoObject. Furthermore, the result is polluted with _rid, Etag, etc. properties that I don't need on my object. It's quite anoying.
I think it's an unnecesary overhead to serialize and then deserialize again, so maybe the optimus way would be to get the raw JSON result and write a method to deserialize directly to Expando.
Or maybe I'm loosing any point and there's an API to get the results as Expandos. Does anybody know it?
Check out this question that I had earlier:
Converting arbitrary json response to list of "things"
Altough I didn't named it, the API in question was actually DocumentDb, so I think you'll be able to use that code.
Seen some bad advice here, but it is built into the SDK natively.
Document doc = cosmosClient.ReadDocumentAsync(yourDocLink);
string json = doc.ToString();

How to deserealize undefined and nested Hashtable structure with json.net

How to deserealize undefined and nested Hashtable structure with json.net?
for example:
#"{"a" : {"b" : 1}}"
Hashtable data = JsonConvert.DeserializeObject<Hashtable>(json_str)
returns Newtonsoft.Json.Linq.JObject at data["a"], instead Hashtable
If the structure of the JSON is not known ahead of time, and the JSON does not contain any type metadata to help Json.Net figure things out, then Json.Net has no choice but to use JTokens (JObject, JProperty, JArray, JValue, etc.) to deserialize things into. That is how it is designed to work.
If you prefer to work with Hashtables, it should be possible to convert the JToken object graph into a nested Hashtable structure after the deserialization, but I'm not sure you gain much by doing that. You would be better off just learning how to read the data out of the JTokens directly. (You would have to do so in order to do the conversion anyway.)

Json.net deserialization of data type ConcurrentBag

I have an object with a property of type ConcurrentBag<object>.
When I am trying to deserialize a json string to my object using JsonConvert.DeserializeObject(), I get an exception which indicates the serializer is failing to convert from an array to the object's ConcurrentBag<object> data type.
Any help to solve this problem would be appreciated.
I think you will have to write a converter of sorts. Kinda like this post:
json.net: specify converter for dictionary keys
This website also has some more examples:
http://weblogs.asp.net/thangchung/archive/2010/08/26/customizing-the-converter-for-json-net.aspx

Are there options with Json.NET that can have it deserialize inconsistent json types into objects?

An example would be that I am consuming json from an api. The api is not consistent in how it returns the json. Say you have an Author and it has a property of Books[]. The api is unfortunately choosing to return Author.Books (of type Book) in cases when there is only one book. The prefered method would be to return just one Book inside Author.Books[].
Json.NET understandably throws a serialization exception when I try to have it deserialize a chunk of json and it finds "Author":{"Book":{... mixed in with "Author":{"Book":["...
Is there a way around this?
Does this answer your question?
Deserializing JSON when sometimes array and sometimes object
I guess the best would be fix the json through a regex replace before sending to the deserializer. If you put here a full sample of a json provided by the API, and a json accepted, i could make the regex for you.

Resources