json of "rates":{"AAA":5.023852,"BBB":77.682521}

At first I did everything manually, in AsyncTask got json , parsed it into my class

 class ModelData { private String name; private Double value; ModelData(String name, Double value) { this.name = name; this.value = value; } } 

and I sqLite the json response itself in the sqLite database. After all List<ModelDate> me in sqLite not to write. Here is another sub-question: do I understand correctly that in order to save an object in sqLite we create a json that repeats this object and save quietly as a string in the database. And there is ORMLite often heard, but now I think that it is needed to store objects in the database. As she does, I don’t know, she can also create json automatically and store it)

And then I decided to implement retrofit 2 (finally it should be) and naturally this is the loss of some flexibility and here’s the problem: my answer is parsed

 @SerializedName("rates") @Expose private List<ModelData> ratesList; 

But how can I save this ratesList to sqLite . I understand that I can somehow convert and save this sheet to Json, but when reading from the database, I will have to parse again, and this parser code is even though I already have one.

This is what I would like: in the POJO model, to have separate and parsed fields and the raw json itself (what kind of annotation is needed to get json entirely? Or what is needed) And also be able to parse the json obtained from the database with the same class POJO model.

I think this is not a bad example of how retrofit 2 and sqLite together deprived of flexibility and for a small project turned out to be redundant and benefit from them only if the project grows.

UPD1 I understood that the interface to rest api can be configured to accept raw json, like this:

 @GET("{date}") Call<JsonObject> getData(@Path("date") String date, @Query("access_key") String accessKey, @Query("base") String base); 

But how can I use the POJO model for this JsonObject later to parse it with a Gson converter added to the retrofit. This is the very thing that I did not understand in this library, where and what method is generally involved in parsing. At what point the converter is connected, maybe somewhere already at the level of okhttp3 ... In general, I was tired of stirring up this library

  • look at subd, such as room or requery - iamthevoid
  • one
    In general, you do not need to parse anything, mark the fields with the annotation serializedname, and return the object as it is from the interface method of the retrofit, it will parse everything "under the hood". If you want to parse json into an object, then use the gson fromJson (string, class) method. new Gson (). fromGson (jsonString, PoJo.class), a typed method, will return the object to you - iamthevoid
  • one
    The generated code is engaged in parsing, read about annotation processors in java. It is possible at the precompilation stage to generate the code that will be compiled and used. This is usually used when it is necessary to automate the process of writing the same type of code - just the parsing of objects by Gson, the View injection by the ButterKnife library, the same retrofit, the room, etc. - iamthevoid pm
  • Well, I do so, mark the fields with annotation and get an object, and then I can’t save this object in sqLite (I don’t want to use another Subd, since everything is done for educational purposes and there is something more important now for studying). Therefore, from the retrofit interface method, I return the raw json to save it in the database, but I forgot about fromJson (string, class). This is what you need to fill the PoJo class with data. - Turalllb
  • But what I have not yet understood is where in the retrofit under the hood that piece of code that PoJo class fills with data .. here it’s probably what I need to deal with annotation processors - Turalllb

1 answer 1

You have some kind of exotic Wishlist from annotations. For the convenience of presenting an object in json, I redefined it to toString in the following way:

  @Override public String toString() { return new Gson().toJson(this); } 

In SQLite you can of course stuff a serialized object, but is it not better to write its fields - each in its own column. The same ORMLite does just that, but without your participation. This will make it possible to make selections based on the values ​​of fields with sorting and other SQL benefits. Although SQLite has its own json-API is still more convenient to work when everything is in its place.

UPD

Retrofit for this purpose, so that its users would not float their brains, but simply indicated the type of model in their interface and took the finished object of this model from Response . If you need to intervene in this process and parse the json into the model somehow in a special way - read the documentation, articles, examples on Gson - it can do a lot of things. https://github.com/google/gson/blob/master/UserGuide.md

  • What you need. I did not know that Gson could introduce the class back to json (although this follows from the fact that I used it for the reverse process). Yes, I understood about why ORMLite is needed. And what about Retrifit, but it will make it easier, but here’s what comes out: the model type is shown in the interface, now I have a class, but the raw json is lost, and this json was needed in sqLite and I have to do it again. It is easier to take in the jsonObject or ResponceBody interface to save it and parse it into a model type. But I understood you well and I just need to get a better look at the functionality of Gson. - Turalllb
  • Something I was wrong in my question. That json that I have refuses to parse into my data model. 'SerializedName ("rates") Expose private List <ModelData> ratesList;' In this matter, everything is already there, so I would not want to create a separate one-type. You can tell what I'm wrong here, if instead of the ModelData list I put HashMap there were all the rules - Turalllb 10:26 pm
  • @SerializedName("rates") @Expose private List<ModelData> ratesList;' Here, finally, I figured out how to select the code in the comments, but I cannot edit the comment above, 5 minutes have passed while I understood. Nevertheless, all in line ... - Turalllb
  • one
    You can not edit, but you can delete unnecessary comments at any time. The object can not be sparsen to the list without custom deserialization. Out of the box Gson can parse the json-object into the map, and the json-array into the array and list. - woesss