1

I would like not to define an extra type just to make the json conversion. I am using a library that needs an object as an input and then performs http operations with this data, so I cannot use a hard coded json string as input.

private static final Gson GSON = new Gson();

public static void main(String[] args) {
  System.out.println(GSON.toJson(new Object() {
    private String email_address = "me@mail.eu";
    public String getEmail_address() {return "me@mail.eu"; }
    public void setEmail_address(String mail) {email_address = mail; }
  }));    
}

I tried to remove getter and setter or leave the getter and remove the field but it doesn't work. Anybody knows how to fix this?

Gismo Ranas
  • 6,043
  • 3
  • 27
  • 39
  • Where is `GSON` declared? – MC Emperor May 17 '18 at 14:13
  • @MCEmperor fixed – Gismo Ranas May 17 '18 at 14:20
  • Why are you using `protected static final Gson GSON = new com.google.gson.GsonBuilder().create();` and not just `protected static final Gson GSON = new Gson();`? Not sure if it would change anything-- just curious. – Alexander Jansing May 17 '18 at 14:35
  • @AlexanderJansing yes, I'm also curious :) maybe it had some parameters and they were deleted. fixed. – Gismo Ranas May 17 '18 at 15:08
  • Ad-hoc anonymous object serialization is prohibited in Gson. See https://github.com/google/gson/blob/master/UserGuide.md#finer-points-with-objects for the official documentation. You're getting `null` because anonymous objects are excluded: https://github.com/google/gson/blob/d84e26d80c39e3b8e34c7e8bbef0ffb35e2947b5/gson/src/main/java/com/google/gson/Gson.java#L228 . Hence, the accepted answer is just misleading. – Lyubomyr Shaydariv May 18 '18 at 07:39

2 Answers2

5

Libraries for Json serialization/deseralization like Gson, count on the fact that you have defined your custom object on which you will map the json string. This is because they use reflection on the class to map the fields with the corresponding keys in the json. Without it, it is difficult that they can achieve anything(usable).

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • How? The official documentation [says](https://github.com/google/gson/blob/master/UserGuide.md#finer-points-with-objects): _Fields corresponding to the outer classes in inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization._ Anonymous classes can be easily introspected using reflection. – Lyubomyr Shaydariv May 18 '18 at 07:47
  • I don't understand your question, can you state it diffferently? – NiVeR May 18 '18 at 09:57
  • Your answer just does not address the real reason or explanation of what's asked by the OP. I've re-read your answer multiple times, but I can't figure out how it answers. The OP is creating an anonymous class object, and Gson _is able_ to use reflection over its as well, but it's prohibited by its design. Ad-hoc anonymous object classes seems to be a sane idea to me if these objects are used in outgoing serialization manner and are never used for deserialization purposes, though. – Lyubomyr Shaydariv May 18 '18 at 11:50
1

Why not define an extra type ? We are not in the 1980s. I would personnally use a DTO. What is Data Transfer Object?

But maybe the answer to you question reside here : Java - Does Google's GSON use constructors?

GabLeg
  • 352
  • 4
  • 14