I have a class which contains some String and int fields. I would like to convert this class to the Map<String, AttributeValue> representation. I know that DynamoDbMapper is doing this internally, but we are not using DynamoDbMapper and I would like to know if there is any existing library that I can use to perform this conversion?
DynamoDB has a mid-level api that you might find helpful. One of its methods is ItemUtils.toAttributeValues(Item). This method allows you to convert from an Item to an attribute value map.
Now, to get an Item, you can construct one manually (but you don’t want to) or you can construct on from a json blob using Item.fromJson(String).
Now all that remains is for you to use your favorite serializer to convert from your java data model to json. The methods I’ve mentioned seamlessly handle the rest of the conversion.
TLDR;
Pojo --> json --> Item --> Map<String, AttributeValue>
Related
I'm trying to use json mappers to handle my Flutter Firebase Firestore serialization and deserialization. Right now I'm using the json_serializable package, which has gotten me past most hurdles.
I'm seeing an issue when inserting / updating though. I have to be able to get my dart object into the json Map form, not the String form to properly insert it. While the library provides a Map<String, dynamic> toJson() => ... function implementation, this function doesn't convert nested objects, which basically makes it worthless.
To work around this I'm converting the json into a String, then back into a Map doing the following:
final json = jsonEncode(reg);
final jsonMap = jsonDecode(json);
This is obviously bad. How can I get my dart object into a json Map form better?
I'm also having to do weird things, like grab the snapshot data, and grab the document id, and then insert the id into the data map before deserializing it, so I have the id in the data model, and then remove it before serializing. There has to be a better way to auto-serialize and deserialize from firebase. What am I missing?
final snapshot = document.data;
snapshot['id'] = documentID;
We are using https://github.com/Azure/amqpnetlite from the Azure team.
The description says we have to use the AmqpContract and AmqpMember attribute classes over the message bus like ActiveMQ.
We also have a lot of classes and we want to migrate to this Amqp library, but is it possible to use the serializer without those attributes and to use our POCO classes directly?
Another workaround could(?) be to serialize or classes into a json and use a wrapper class to send our payload:
[AmqpContract]
public class OurMessage
{
[AmqpMember]
public string JsonPayload {get;set;}
}
or should we avoid this workaround?
The POCO classes as you mentioned are not supported right now. You will need to pick another serializer. Once you have the JSON string or a byte array, you do not need to define a wrapper class. Just directly create an AMQP message with the payload and get it back from the message body on the receiver side.
// sender
var msg = new Message(jsonPayload);
sender.Send(msg);
// receiver
var msg2 = receiver.Receive();
var jsonPayload = msg2.GetBody<string>();
Same thing can be done for byte array.
The AMQP serializer is designed to be simple and everything has to be explicitly annotated. For most cases this is possible and it helps to avoid double encoding and enable easier inter-op with other AMQP clients.
I use CouchbaseRepository in my project but sometimes I use lower level couchbase sdk methods to retrieve JsonDocument. Is there a way I can use spring-data-couchbase to convert a JsonDocument to lets say a User?
This is all done internally in CouchbaseTemplate in the method private <T> T mapToEntity(String id, Document<String> data, Class<T> entityClass)
But as you can see it's private so I can't call it myself.
You can create a wrapper which uses Jackson to convert the JsonDocument to any object you want.
mapper.readValue(doc.content().toString(), SomeClass.class);
The first argument is the JsonDocument, the content method is what contains the actual Json.
I have a WCF Rest Service. I would like to use it from Android device. To do this I have chosen gson library. Everything seemed to work fine until I wanted to return from my service Object Containing Map. Gson serializes it (and as I am assuming also tries to deserialize from it) as something like:
{"org.Mob.ComplexObject#3dac2f9c":"TAX1","org.Mob.ComplexObject#7369ca65":"TAX2"}
But the json sent by my service looks like:
{"Key":
{"DefaultValue":"True",
"Description":null,
"DisplayName":"Custom Boolean",
"FieldType":0,
"Id":6,
"IsReadOnly":false,
"IsRequired":false,
"MaxLength":null,
"Name":"BoolVal",
"ParamType":0},
"Value":"True"},
{"Key":
{"DefaultValue":"",
"Description":null,
"DisplayName":"Custom Text",
"FieldType":4,
"Id":7,
"IsReadOnly":false,
"IsRequired":true,
"MaxLength":16,
"Name":"TextVal",
"ParamType":0},
"Value":"sda"}
}
Gson does not have a built-in feature to serialize a Java map into JSON formatted as desired. (The feature to handle complex map keys does not address this.) Custom serialization/deserialization processing is necessary.
The other way to do it could be adding you own implementation of Map / Dictionary which extends ArrayList, and in fact is a List of KVPair, where KVPair looks like:
class KVPair<K,V>{
K Key;
V Value;
}
I don't know if it's elegant, but it worked in my case.
Thought Id ask here before jumping into a problem on the Blackberry Playbook (Adobe Flex)
I have a search form and a SearchCriteria class representing the search form's input, for example:
public class SearchCriteria
{
private var firstname:String;
private var surname:String;
public function SearchCriteria()
{}
public function getFirst():String{
return firstname;
}
...
As the person fills out the form, I would like to cache a copy in memory of the SearchCriteria so that if there is a problem, or the user turns off their tablet, I could recreate the form when they log back in.
TL;dr version: Basically, what is the best way to Serialize and Deserialize objects in Actionscript 3? (particularly on the Blackberry Playbook)
Thanks
Phil
If you are asking specifically about serialization and deserialization I'd recommend AMF3 format.
It is used by SharedObject mentioned by Timofei Davydik. You can also serialize objects with ByteArray.writeObject() and then save the ByteArray to a file.
Note that if you want to use strongly typed object (recommended) you should annotate your model classes with [RemoteClass] metadata. If you don't want some properties to be serialized use [Transient] metadata.
You could serialize them to JSON format using the as3corelib library.
And then use SharedObject or SQLite (works well in Playbook).
Read about SharedObject class.