gson

Is there a way of GSON to be “not lenient” at all?

陌路散爱 提交于 2019-12-12 12:50:24
问题 It seems that GSON silently ignores when a JSON string contains field names that don't match the target POJO class. One solution outlined here suggests to use annotations to mark "required" fields to have GSON fail when de-serializing strings that don't contain fields. But we defined that our POJOs must be "exact" matches (when we allow for incoming objects to be null, they must be declared as Optional field in the POJO - and we have a special type adapter that turns nulls into Optional.empty

How to receive JSON object in Ktor?

爷,独闯天下 提交于 2019-12-12 12:01:26
问题 I have data class defined, configured gson and created rout to handle post request as follows: data class PurchaseOrder(val buyer: String, val seller: String, val poNumber: String, val date: String, val vendorReference: String) install(ContentNegotiation) { gson { setDateFormat(DateFormat.LONG) setPrettyPrinting() } post("/purchaseOrder"){ val po = call.receive<PurchaseOrder>() println("purchase order: ${po.toString()}") call.respondText("post received", contentType = ContentType.Text.Plain)

Why has gson does not allow serialization of java.lang.Class?

社会主义新天地 提交于 2019-12-12 11:34:34
问题 If you try to serialize an object that has a field of type java.lang.Class , serializing it will lead to java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: <some_class> Forgot to register a type adapter Below is the code snippet from com.google.gson.internal.bind.TypeAdapters.java public final class TypeAdapters { . . . public static final TypeAdapter<Class> CLASS = new TypeAdapter<Class>() { @Override public void write(JsonWriter out, Class value) throws

Gradle Javadoc fails with “error: package … does not exist”

江枫思渺然 提交于 2019-12-12 10:38:38
问题 Our Gradle build script fails when building Javadocs for Android Studio project for a library we develop, with errors like: /MY_LOCAL_PATH/MyClass.java:5: error: package com.google.gson does not exist import com.google.gson.Gson; Details: We are building an Android library with Gradle , and want to build the Javadocs for it. The Android Studio project is configured to require a Maven dependency for GSON , although I'm guessing it would happen with every dependency whose lib file is not

how to deserialize some specific fields only from Json using Gson?

拈花ヽ惹草 提交于 2019-12-12 10:18:43
问题 I have the following JSON string [ { "channel": "/bvmt/initValues", "data": { "value": { "instrumentIds": "['TN0007250012','TN0007500010']", "instruments": "[{'mnemonic':'ADWYA','marche':'ADWYA','phaut':5.82,'open':5.82,'nbrTrans':7,'veille':5.82,'time':'11:14:28','recapChange':0.00,'state':'','variation':'.','ref':5.82,'stateGrp':'S','percentChange':-0.34,'last':5.80,'bestTransaction':[{'value':5.82,'qte':'3','time':'10:00:00'},{'value':5.82,'qte':'5','time':'10:02:26'},{'value':5.82,'qte':

Reflection Permission problems when using the GSON library in a applet

别等时光非礼了梦想. 提交于 2019-12-12 09:56:21
问题 I'm writing an Applet that makes some JSON-RPC calls. I'm using the Google JSON library (GSON) to cast the response JSON into a class. Thsi seems to work fine as is but when I use this code in my Applet, I'm hit with a java.lang.reflect.reflectpermission . From what I read on this thread on SO, it seems that since GSON uses Reflection, I cannot use it in Applets unless I explicitly modify the security policy. How can I get around this? I've created a bunch of classes in my application and was

How to parse a json file into a java POJO class using GSON

萝らか妹 提交于 2019-12-12 09:55:02
问题 I am using GSON to parse a JSON file and i want to map this JSON object to a POJO class. The problem is the property name in JSON doesn't have camel-case but my java POJO object having camel-case property name. Is there any idea without having any performance hit? Eg: attribute name in JSON file is 'OrderNumber', But in my POJO class , Instead ordernumber, i have 'salesNumber' as attribute name. Now how can we map that OrderNumber from JSON to salesNumber of POJO class? Thanks in advance! 回答1

Use a custom deserializer only on certain fields?

喜夏-厌秋 提交于 2019-12-12 09:39:32
问题 With gson, is it possible to use a custom deserializer / serializer only on certain fields? The user guide shows how to register an adapter for an entire type, not for specific fields. The reason why I want this is because I parse a custom date format and store it in a long member field (as a Unix timestamp), so I don't want to register a type adapter for all Long fields. Is there a way to do this? 回答1: I also store Date values as long in my objects for easy defensive copies. I also desired a

Parsing Json Feeds with google Gson

假装没事ソ 提交于 2019-12-12 09:38:40
问题 I would like to know how to parse a JSON feed by items (eg. url / title / description for each item). I have had a look to the doc / api but, it didn't help me. This is what I got so far import com.google.gson.Gson; import com.google.gson.JsonObject; public class ImportSources extends Job { public void doJob() throws IOException { String json = stringOfUrl("http://feed.test/all.json"); JsonObject jobj = new Gson().fromJson(json, JsonObject.class); Logger.info(jobj.get("responseData").toString

How to serialize Date to long using gson?

a 夏天 提交于 2019-12-12 08:27:16
问题 I recently switched some of our serialization from Jackson to Gson . Found out that Jackson serializes dates to longs. But, Gson serializes Dates to strings by default. How do I serialize dates to longs when using Gson? Thanks. 回答1: First type adapter does the deserialization and the second one the serialization. Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))