0

JSON-B converts types like LocalDate to/from a simple String. When I have a Type, how can I find out if JSON-B will convert this type directly, i.e. if it's a scalar type?

I don't have an instance, so I can't even just try to convert it to a String and see if it contains curly braces.

Or do I have to look for types that have a valueOf or parse method or a Constructor having exactly one String parameter?

rü-
  • 2,129
  • 17
  • 37
  • The answer depends on which JSON library you are using, so your question cannot be answered. --- *FYI:* Most classes can be scalar types. If a string representation can be generated for the class, and that can be used to re-instantiate the object, then the class *can* be scalar as far as JSON is concerned, either by following conventions, or by registering (de)serializers with the JSON framework. – Andreas Mar 29 '20 at 11:24
  • I'm not an expert in JSON but the generic approach would be a try-catch szenarion. – tifi90 Mar 29 '20 at 11:24
  • @tifi90 I think they want to know *a priori*, not *a posteriori*. – Federico klez Culloca Mar 29 '20 at 11:44
  • I use JSON-B (JSR 365), and I don't have an instance nor a JSON string available. I'm working on a meta level, so to speak. – rü- Mar 29 '20 at 15:23

1 Answers1

0

It's possible to check ahead of time if a given type can be serialized to JSON (i.e. convert from strongly typed to weakly typed format), but there isn't a way to check if a given type can be deserialized to a POJO until you have an actual JSON value at runtime (i.e. convert from weakly typed format to strongly typed format).

In general, the rules for a type being serializable are simply:

  • The class must be accessible (public or protected)
  • If the class is an inner class it must be static
  • Zero or more public fields/getters that contain/return serializable types

In general, the rules for a type being deserializable are:

  • The class must be accessible (public or protected)
  • If the class is an inner class it must be static
  • Zero or more public fields/setters that contain/return deserializable types
  • The class must have a public no-args constructor (or a ctor annotated with @JsonbCreator)
  • OR The class must have a public static factory method annotated with @JsonbCreator

You may find that what constitutes a "convertible type" is a very broad criteria in JSON-B (or any other Java JSON library). For example, the JSON string {} can be deserialized into any Type that has a public no-args constructor. Likewise, a Java class that has no public fields or getter methods will serialize to the JSON string {}.

Since a convertible type is such a broad term, it would be helpful if you specified a bit further what you are trying to achieve -- maybe there is a more specific approach I could suggest.


Additional info:

Many of the commonly used classes from the JDK are supported. See sections 3.3 - 3.5 of the JSON-B spec for the full list. Aside from the built-in classes, requirements are specified in section 3.7 of the JSON-B spec.

For deserialization there are more requirements than serialization. They are described in detail in section 3.7 and section 4.5.

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • My question was about types that don't have any fields but are (de)serialized from/to a simple JSON String. – rü- Mar 29 '20 at 17:21