Skip to content
Advertisement

Tag: gson

Gson to append json object within an array in a json file in java

I am trying to store a game result in a JSON file. The results are stored when the game is over, and are supposed to be displayed afterwards in a table. I have this function that creates the game score and adds it to json file using GSON That method creates a JSON file that looks like this: The problem

How to set the classpath correctly in java

I need to compile and run simple code using the gson library, but I can’t use Maven, Gradle or the IDE. The directory contains Main.java and gson-2.9.0.jar javac -cp gson-2.9.0.jar Main.java works correctly and creates Main.class But when I run java -cp ./*: Main, I get I also tried the following commands: But all these commands give the same result.

Streaming large JSON from input stream efficiently in Java

In order to save memory and avoid an OOM error, I want to stream a large JSON from an input stream and extract the desired things from it. More exactly, I want to extract and save some strings from that JSON: files.content.fileContent.subList.text = “some text in file” files.content.fileContent.subList.text = “some text in file2” and save them into a String variable:

Java Date to MongoDB date with Gson

Introduction I am currently working on a project that regularly saves several java objects to a MongoDB database. This object contains a Date. Problem The conversion of java Date to Json Mongo give this: But this format does not conform with MongoDB Date. MongoDB is considered as a string instead of a Date. Affected code Question How to create an

GSON flat down map to other fields

So I have an Android app which uses Retrofit for API. I have a class like which looks like: When GSON creates a JSON it looks like: Would it be possible to change JSON serialization to: Thanks. Answer Here is how Gson could be used to implement the flattening: I’ve put some comments explaining “whats” and “hows”. But it would

IllegalArgumentException: Unable to create @Body converter for class

Whenever I try to create a request with Retrofit it fails. The strange thing is that it worked before, I didn’t change anything about the code. Now months later it doesn’t work anymore. I’m getting the following error: IllegalArgumentException: Unable to create @Body converter for class apis.Config (parameter #2) I tried changing from Gson to Moshi but it didn’t solve

Advertisement