Friday, July 13, 2018

How to include class name property when serializing json

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
 * Only for List type fields.
 * @author alexeyz
 */
public class RuntimeClassNameTypeAdapter extends TypeAdapter> {

private static final String CLASS_NAME_PROPERTY = "type";

@Override
public void write(JsonWriter out, List src) throws IOException {
if (src == null) {out.nullValue();return;}

Gson gson = new Gson();
out.beginArray();
for (Object srcBlocker : src) {
        JsonElement serializedBlocker = gson.toJsonTree(srcBlocker);
        JsonObject blocker = (JsonObject) serializedBlocker;
        blocker.addProperty(CLASS_NAME_PROPERTY, srcBlocker.getClass().getSimpleName());
        out.jsonValue(blocker.toString());
}
out.endArray();
}



Usage:
    @JsonAdapter(RuntimeClassNameTypeAdapter.class)

    protected List book;


Monday, May 7, 2018

Http java links

https://www.programcreek.com/java-api-examples/?api=io.reactivex.netty.protocol.http.client.HttpClient

https://blogs.oracle.com/java/jdk-http-client#synchronousGet

http://www.baeldung.com/java-http-request

https://stackoverflow.com/questions/46387896/how-can-i-use-http-client-apisince-java-9-in-java8-project

https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

http://openjdk.java.net/groups/net/httpclient/intro.html

https://alvinalexander.com/java/java-apache-httpclient-restful-client-examples

https://github.com/timboudreau/netty-http-client

https://github.com/cloudbow/netty-http-client

TODO mongodb query toJson



Operator $all produces error on toJson() method.

http://api.mongodb.com/java/current/org/bson/codecs/package-summary.html

https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver

http://api.mongodb.com/java/3.0/?com/mongodb/client/model/Filters.html

http://mongodb.github.io/mongo-java-driver/3.4/bson/codecs/


Criteria criteria = Criteria.where("props").all(
new Criteria().elemMatch(Criteria.where("key").is("lang").and("value").is("EN")),
new Criteria().elemMatch(Criteria.where("key").is("photo").and("value").exists(true)));
String json = criteria.getCriteriaObject().toJson();



org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.Criteria.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184)


Documentation: $all with $elemMatch

If the field contains an array of documents, you can use the $all with the $elemMatch operator.
The following operation queries the inventory collection for documents where the value of the qty field is an array whose elements match the $elemMatch criteria:
db.inventory.find( {
                     qty: { $all: [
                                    { "$elemMatch" : { size: "M", num: { $gt: 50} } },
                                    { "$elemMatch" : { num : 100, color: "green" } }
                                  ] }
                   } )