Converting a JsonDocument to its mapped entity - spring-data-couchbase

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.

Related

Why is dataSnapshot deserialized in ViewModel and not in class extending LiveData?

I read these blog posts written by Doug Stevenson Part 1, Part 2, Part 3. On part 2 he says that
The Realtime Database SDK makes it really easy to convert a DataSnapshot into a JavaBean style object
And mentions that we can use this line of code to get the DataSnapshot deserialized into a HotStock object (a JavaBean class)
HotStock stock = dataSnapshot.getValue(HotStock.class)
I´m confused because he first uses Transformation.map and then he says that if a LiveData transformation is expensive we can create a MediatorLiveData object from the ViewModel to get the DataSnapshot converted into a HotStock object.
My question is: Why can´t we just call the
HotStock stock = dataSnapshot.getValue(HotStock.class)
from the onDataChange() method on the ValueEventListener which resides in the class extending the LiveData super class and then simply use the setValue() method to pass the HotStock object directly to the ViewModel and then to the Activity or Fragment observing for changes to the LiveData?
You can do that, but getValue() passing a class object is actually kind of slow (especially the first time, for a particular class) because the SDK has to use reflection to figure out how to map all the fields into the object it creates. Java reflection known to be fairly slow. But it's up to you what you want to use.

Convert a Java class to DynamoDb Map<String, AttributeValue>

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>

How to 'package' request parms into one object

If you have a controller method which has a whole bunch of parameters such as:
getSomething (String a, int b, Timestamp c, etc...)
what is the best way to replace those with an object, so you do not have to then instantiate a new object and set all those from the parameters, like so:
SomeObject o = new SomeObject();
o.setA(a);
o.setB(b);
o.setC(c);
etc..
// use o on other method calls to prepare a response
I have tried #ModelAttribute like so:
getSomething (#modelAttribute SomeObject o)
but it does not seem to work well unless you only use simple types for properties such as strings and ints. A Timestamp property for example causes it to fail with a "Bad Request"
#RequestBody could work, but with some pretty significant changes required to your method call which ends up that you can no longer simply call that method by the browser anymore to test it, which makes development much more annoying.
Is there no way to tell Spring to accept a bunch of parameters in a regular http GET request and use them to instantiate an object for you with the ability to work with all property data types?
You probably need to write a type converter for the Timestamp class, and register it with Spring MVC ConversionService.
See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
Why can't you use a Date or DateTime (Joda Time) class?
Make sure you use the #Timestamp annotation if using JPA

Create key with objectify

Calling Objectify method:
Key.create(ParentClass.class parentClass, KindClass.class kindClass, String id)
for the exactly same parameters twice at the same time, I got:
name=UWxoCs7KpxDu2fYBI3s2fkOq-wM
name=jOqQzhZzAScJ0O3OEtYF3jzb34Q
Does this method need to run in a transaction so I get a predictable result?
The app id and namespace are also encoded in the key. You can see the components that represent a stable key here:
https://code.google.com/p/googleappengine/source/browse/trunk/java/src/main/com/google/appengine/api/datastore/Key.java
I cannot find any reference to an Objectify Key.createKey method. Which version of Objectify are you using?
There is however a Key.create(Key<?> parent, java.lang.Class<? extends T> kindClass, java.lang.String name), trying using that and let us know how you get on.
Here is the API page for Key https://objectify-appengine.googlecode.com/svn/trunk/javadoc/com/googlecode/objectify/Key.html

Storage and Serialization in AS3 / Adobe Flex

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.

Resources