question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@SerializedName annotation for defining Class/Field names

See original GitHub issue

We need something similar to GSON’s SerializedName annotation which can map a different name field from JSON string to RealmObject’s property.

eg:

{ "dog_name": "abc"}
class Dog {
    @SerializedName("dog_name")
    private String name;
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:18
  • Comments:34 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
cmelchiorcommented, Oct 12, 2016

I have the same feeling, but it gets a bit tricky, because cross-platform schemas are a big topic for us right now 😄

public class Foo extends RealmObject {

  // This is definitely eecky
  @SerializedName("my_stupid_json_name")
  @FieldName("internal_name")
  private String name;
} 

Thinking about this. Maybe this is actually the best time to actually split our JSON API away from Realm, because a @SerializedName for JSON would only make sense if you used our JSON API, if you use e.g GSON it would just be confusing that Realm also had a similar annotation.

Instead we should create adapters for the various JSON imports:

- `io.realm:android-json-adapter:1.0.0` : Basically our existing API just moved to a seperate dependency. This library could then include specialized annotations like `@SerializedName`

- `io.realm:gson-adapter:1.0.0` : GSON type adapters

- `io.realm:android-streaming-json-adapter:1.0.0` : Our API 11 streaming adapters

Alternatively they get configured through the new realm closure block introduced in 2.0

It would have the following implications:

  • All createFromJson* and createOrUpdateFromJson* methods would be removed from our current public API.
  • JSON Support is entirely opt-in. Right now they are a massive chunck of our proxy classes, we could strip a lot of code.
  • We will have to find another way of calling JSON methods. GSON/Moshi/Jackson can continue to use our copyToRealm, but we need somewhere to hook “standard” JSON output in. Where isn’t clear to me.
3reactions
beeendercommented, Oct 13, 2016

Split the json to another project would be great, especially we will have a chance to switch to javapoet from there. But the new project probably needs to be more generic to support other data converters than JSON, eg. flatbuffer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gson @SerializedName - Change field names example
Gson @SerializedName is used to change the name of fields between JSON and Java objects while the serialization and deserialization process.
Read more >
Multiple GSON @SerializedName per field? - Stack Overflow
Reason: By default Deserialization is managed with a LinkedHashMap and the keys are defined by incoming json's field names (not the custom class's...
Read more >
@SerializedName annotation for defining Class/Field names -
We need something similar to GSON's SerializedName annotation which can map a different name field from JSON string to RealmObject's property.
Read more >
What to use @SerializedName annotation using Gson in Java?
The @SerializedName annotation can be used to serialize a field with a different name instead of an actual field name.
Read more >
How to read and write JSON using Gson in Java
JSON Field Naming with @SerializedName Annotation. By default, Gson uses the class field names as JSON property names.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found