![Page 1: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/1.jpg)
![Page 2: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/2.jpg)
![Page 3: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/3.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
What’s new in JSR 367 Java API for JSON BindingDmitry Kornilov EclipseLink MOXy & SDO Team Lead Oracle Czech
![Page 4: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/4.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
4
![Page 5: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/5.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5
Program Agenda
1. What is JSONB?
2. JSR Status & Progress
3. What is in the spec
4. The Implementation (with demo)
5. What’s next
6. Q&A session
![Page 6: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/6.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6
• JSON Binding is a standard• JSON Binding = JSON-B = JSONB = JSR 367• It’s about converting Java objects to and from JSON documents
What is JSON Binding?
6
![Page 7: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/7.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 7
What is JSON Binding?
7
public class Customer { public int id; public String firstName; public String lastName; ….}
Customer e = new Customer();e.id = 1;e.firstName = “John”;e.lastName = “Doe”;
{ "id": 1, "firstName" : "John", "lastName" : "Doe",}
Java JSON
![Page 8: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/8.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 88
Usage in Other Frameworks
JAX-RS
Objects
XML
JSON
JAXB
JSON-B
8
![Page 9: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/9.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSR Status & Progress
9
![Page 10: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/10.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1010
JSR 367 Statushttps://www.jcp.org/en/jsr/detail?id=367
![Page 11: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/11.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1111
What’s Done in Last Year• Finished experts group formation• Early Draft published• Reference implementation started
![Page 12: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/12.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1212
Experts Group
![Page 13: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/13.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1313
Early Draft• Created 20 Aug 2015• Published 19 Sep 2015
![Page 14: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/14.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1414
Reference Implementation Started
First JSONB marshaller implementation Signed-off-by: Dmitry Kornilov <[email protected]>Reviewed-by: Martin Grebac, Lukas Jungmann
http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/log/
author Dmitry Kornilov 2015-09-14 09:06:06 (EDT)committer Dmitry Kornilov 2015-09-14 09:06:18 (EDT)
![Page 15: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/15.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1515
![Page 16: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/16.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON-B Specification (Early Draft)
16
![Page 17: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/17.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1717
Specification Project• Project Home:
https://java.net/projects/jsonb-spec/pages/Home• Sources & samples:
https://java.net/projects/jsonb-spec/sources/git/show/api• Document (pdf):
https://java.net/projects/jsonb-spec/sources/git/content/spec/spec.pdf
![Page 18: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/18.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1818
JSON-B Runtime API1. Create JSONB engine configuration (optional)2. Create JSONB engine3. Use it!
![Page 19: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/19.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 1919
JSON-B Engine Configuration
import javax.json.bind.JsonbConfig;
// JSON-B engine configuration JsonbConfig config = new JsonbConfig() .withFormatting(…) .withNullValues(…) .withEncoding(…) .withStrictIJSON(…) .withPropertyNamingStrategy(…) .withPropertyOrderStrategy(…) .withPropertyVisibilityStrategy(…) .withAdapters() .withBinaryDataStrategy(…);
• Configuration is optional• Managed by one class• Builder pattern is used• Passed to JsonbBuilder
![Page 20: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/20.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2020
JSON-B Engine• Jsonb class• Created by JsonbBuilder• Ability to choose JSONP
provider
import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder;
// Create JSON-B engine Jsonb jsonb = JsonbBuilder.newBuilder() .withConfig(…) .withProvider(…) .build();
// Create with given config Jsonb jsonb = JsonbBuilder.create(config);
// Create with default config Jsonb jsonb = JsonbBuilder.create();
![Page 21: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/21.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
• toJson(…)• fromJson(…)
2121
JSON-B Engine
String toJson(Object object); String toJson(Object object, Type runtimeType); void toJson(Object object, Appendable appendable); void toJson(Object object, Type runtimeType, Appendable appendable); void toJson(Object object, OutputStream stream); void toJson(Object object, Type runtimeType, OutputStream stream);
<T> T fromJson(String str, Class<T> type); <T> T fromJson(String str, Type runtimeType); <T> T fromJson(Readable readable, Class<T> type); <T> T fromJson(Readable readable, Type runtimeType); <T> T fromJson(InputStream stream, Class<T> type); <T> T fromJson(InputStream stream, Type runtimeType);
![Page 22: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/22.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2222
Default Mapping• Basic Types• Specific Types• Dates• Classes• Collections/Arrays• Enumerations• JSON-P
import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder;
// Create with default config Jsonb jsonb = JsonbBuilder.create();
![Page 23: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/23.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2323
• java.lang.String• java.lang.Character• java.lang.Byte (byte)• java.lang.Short (short)• java.lang.Integer (int)• java.lang.Long (long)• java.lang.Float (float)• java.lang.Double (double)• java.lang.Boolean (boolean)• java.lang.Number
Default Mapping - Basic Types
![Page 24: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/24.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2424
• java.lang.String• java.lang.Character• java.lang.Byte (byte)• java.lang.Short (short)• java.lang.Integer (int)• java.lang.Long (long)• java.lang.Float (float)• java.lang.Double (double)• java.lang.Boolean (boolean)• java.lang.Number
Long longVal = Long.valueOf(1L); String json = jsonb.toJson(longVal);
// Corresponding toString() method is used on serializationjson.equals(longVal.toString()); // true
Default Mapping - Basic Types
![Page 25: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/25.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2525
• java.lang.String• java.lang.Character• java.lang.Byte (byte)• java.lang.Short (short)• java.lang.Integer (int)• java.lang.Long (long)• java.lang.Float (float)• java.lang.Double (double)• java.lang.Boolean (boolean)• java.lang.Number
Long longVal = Long.valueOf(1L); String json = jsonb.toJson(longVal);
// Corresponding toString() method is used on serializationjson.equals(longVal.toString()); // true
// Corresponding parseX() method is used on deserializationjsonb.fromJson(json, Long.class) .equals(Long.parseLong(json)); // true
Default Mapping - Basic Types
![Page 26: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/26.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2626
• java.lang.String• java.lang.Character• java.lang.Byte (byte)• java.lang.Short (short)• java.lang.Integer (int)• java.lang.Long (long)• java.lang.Float (float)• java.lang.Double (double)• java.lang.Boolean (boolean)• java.lang.Number
Long longVal = Long.valueOf(1L); String json = jsonb.toJson(longVal);
// Corresponding toString() method is used on serializationjson.equals(longVal.toString()); // true
// Corresponding parseX() method is used on deserializationjsonb.fromJson(json, Long.class) .equals(Long.parseLong(json)); // true
jsonb.toJson("string"); // “string” jsonb.toJson('\uFFFF'); // "\uFFFF\" jsonb.toJson((byte)1); // 1 jsonb.toJson((short)1); // 1 jsonb.toJson((int)1); // 1 jsonb.toJson(1L); // 1 jsonb.toJson(1.2f); // 1.2 jsonb.toJson(1.2); // 1.2 jsonb.toJson(true); // true jsonb.toJson(null); // null
Default Mapping - Basic Types
![Page 27: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/27.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2727
Default Mapping - Specific Types• java.math.BigInteger• java.math.BigDecimal• java.net.URL• java.net.URI• java.util.Optional• java.util.OptionalInt• java.util.OptionalLong• java.util.OptionalDouble
![Page 28: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/28.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2828
Default Mapping - Specific Types• java.math.BigInteger• java.math.BigDecimal• java.net.URL• java.net.URI• java.util.Optional• java.util.OptionalInt• java.util.OptionalLong• java.util.OptionalDouble
BigDecimal bdVal = BigDecimal.valueOf(1.2); String json = jsonb.toJson(bdVal); // 1.2
// Corresponding toString() method is used on serializationjson.equals(bdVal.toString()); // true
![Page 29: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/29.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2929
Default Mapping - Specific Types• java.math.BigInteger• java.math.BigDecimal• java.net.URL• java.net.URI• java.util.Optional• java.util.OptionalInt• java.util.OptionalLong• java.util.OptionalDouble
BigDecimal bdVal = BigDecimal.valueOf(1.2); String json = jsonb.toJson(bdVal); // 1.2
// Corresponding toString() method is used on serializationjson.equals(bdVal.toString()); // true
// Constructor with string argument is used on deserializationjsonb.fromJson(json, BigDecimal.class) .equals(new BigDecimal(1.2)); // true
![Page 30: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/30.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3030
Default Mapping - Specific Types• java.math.BigInteger• java.math.BigDecimal• java.net.URL• java.net.URI• java.util.Optional• java.util.OptionalInt• java.util.OptionalLong• java.util.OptionalDouble
BigDecimal bdVal = BigDecimal.valueOf(1.2); String json = jsonb.toJson(bdVal); // 1.2
// Corresponding toString() method is used on serializationjson.equals(bdVal.toString()); // true
// Constructor with string argument is used on deserializationjsonb.fromJson(json, BigDecimal.class) .equals(new BigDecimal(1.2)); // true
// OptionalInt jsonb.toJson(OptionalInt.of(1)); // 1 jsonb.toJson(OptionalInt.empty()); // null
![Page 31: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/31.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3131
Default Mapping - Dates
ISO_DATE ISO_DATE_TIME
Java Type Format
java.util.Date ISO_DATE_TIME
java.util.Calendar ISO_DATE if to time information present, otherwise ISO_DATE_TIME
java.util.GregorianCalendar ISO_DATE if to time information present, otherwise ISO_DATE_TIME
java.util.TimeZone NormalizedCustomId (see TimeZone javadoc)
java.util.SimpleTimeZone NormalizedCustomId (see TimeZone javadoc)
java.time.Instant ISO_INSTANT
java.time.LocalDate ISO_LOCAL_DATE
java.time.LocalTime ISO_LOCAL_TIME
java.time.LocalDateTime ISO_LOCAL_DATE_TIME
java.time.ZonedDateTime ISO_ZONED_DATE_TIME
java.time.OffsetDateTime ISO_OFFSET_DATE_TIME
java.time.OffsetTime ISO_OFFSET_TIME
java.time.ZoneId NormalizedZoneId as specified in ZoneId javadoc
java.time.ZoneOffset NormalizedZoneId as specified in ZoneOffset javadoc
java.time.Duration ISO 8601 seconds based representation
java.time.Period ISO 8601 period representation
![Page 32: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/32.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3232
// java.util.DateSimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Date parsedDate = sdf.parse("25.10.2015"); jsonb.toJson(parsedDate)); // ”2015-10-25T00:00:00"
Default Mapping - Date Samples
![Page 33: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/33.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3333
// java.util.DateSimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Date parsedDate = sdf.parse("25.10.2015"); jsonb.toJson(parsedDate)); // ”2015-10-25T00:00:00"
// java.util.CalendarCalendar dateCalendar = Calendar.getInstance(); dateCalendar.clear(); dateCalendar.set(2015, 10, 25); jsonb.toJson(dateCalendar); // ”2015-10-25”
Default Mapping - Date Samples
![Page 34: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/34.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3434
// java.util.DateSimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Date parsedDate = sdf.parse("25.10.2015"); jsonb.toJson(parsedDate)); // ”2015-10-25T00:00:00"
// java.util.CalendarCalendar dateCalendar = Calendar.getInstance(); dateCalendar.clear(); dateCalendar.set(2015, 10, 25); jsonb.toJson(dateCalendar); // ”2015-10-25”
// java.time.Instantjsonb.toJson(Instant.parse(“2015-10-25T23:00:00Z")); // ”2015-10-25T23:00:00Z”
// java.time.Durationjsonb.toJson(Duration.ofHours(5).plusMinutes(4)); // “PT5H4M"
// java.time.Periodjsonb.toJson(Period.between( LocalDate.of(1960, Month.JANUARY, 1), LocalDate.of(1970, Month.JANUARY, 1))); // "P10Y"
Default Mapping - Date Samples
![Page 35: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/35.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3535
Default Mapping - Classes• Public and protected nested and static nested classes• Anonymous classes (serialization only)• Inheritance is supported• Default no-argument constructor is required for deserialization
![Page 36: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/36.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3636
Default Mapping - Fields• Final fields are supported (serialization only)• Static fields are not supported• Transient fields are not supported• Null fields are skipped• Lexicographical order• Parent class fields are serialized before child class fields
![Page 37: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/37.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3737
Fields Order Sample{ "parentA": 1, "parentB": 2 }
public class Parent { public int parentB; public int parentA; }
![Page 38: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/38.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3838
Fields Order Sample{ "parentA": 1, "parentB": 2 }
{ "parentA": 1, "parentB": 2, "childA": 3, "childB": 4 }
public class Parent { public int parentB; public int parentA; }
public class Child extends Parent { public int childB; public int childA; }
![Page 39: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/39.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3939
Default Mapping - Scope and Field Access Strategy
Serialization• Existing fields with public getters• Public fields with no getters• Public getter/setter pair without
a corresponding field
Deserialization• Existing fields with public setters• Public fields with no setters• Public getter/setter pair without
a corresponding field
![Page 40: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/40.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4040
Scope and Field Access Strategy Sample{ "publicFinalField": 1,
"publicFieldWithNoGetter": 1,
"privateFieldWithPublicGetter": 1,
"noField": 1, }
public class Foo { public final int publicFinalField; private final int privateFinalField;
public static int publicStaticField; public int publicFieldWithNoGetter; public int publicFieldWithPrivateGetter; public Integer publicNullField = null;
private int privateFieldWithNoGetter; private int privateFieldWithPublicGetter;
public int getNoField() {}; public void setNoField(int value) {}; }
![Page 41: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/41.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4141
// Array int[] intArray = {1, 2, 3}; jsonb.toJson(intArray); // [1,2,3]
// CollectionCollection<Object> list = new ArrayList<>(); list.add(1); list.add(2); list.add(null); jsonb.toJson(list); // [1,2,null]
// MapMap<String, Object> map = new LinkedHashMap<>(); map.put("first", 1); map.put("second", 2); jsonb.toJson(map); // {"first":1,"second":2}
Default Mapping - Arrays/Collections• Collection• Map• Set• HashSet• NavigableSet• SortedSet• TreeSet• LinkedHashSet• TreeHashSet• HashMap• NavigableMap• SortedMap
• TreeMap• LinkedHashMap• TreeHashMap• List• ArrayList• LinkedList• Deque• ArrayDeque• Queue• PriorityQueue• EnumSet• EnumMap
![Page 42: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/42.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4242
public enum Language { English, Russian, Czech}
jsonb.toJson(Language.English); // "English"
Default Mapping - Enumerations• Serialization:
name()• Deserialization:
valueOf(String)
![Page 43: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/43.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4343
// JsonObject JsonBuilderFactory factory = Json.createBuilderFactory(null); JsonObject jsonObject = factory.createObjectBuilder() .add(“name", "Jason") .add(“city", "Prague") .build(); jsonb.toJson(jsonObject); // {“name":"Jason","city":"Prague"}
// JsonValuejsonb.toJson(JsonValue.TRUE); // true
Default Mapping - JSON-P• Supported types:
• javax.json.JsonArray• javax.json.JsonStructure• javax.json.JsonValue• javax.json.JsonPointer• javax.json.JsonString• javax.json.JsonNumber
• Serialization: javax.json.JsonWriter
• Deserialization:javax.json.JsonReader
![Page 44: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/44.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Reference Implementation Demo
44
![Page 45: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/45.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4545
Jason’s Plans• Web: http://159.203.248.103:8080• Sources: https://github.com/m0mus/jason_plans• Demonstrates serialization of this class:public class JasonPlans { public Date date; public int id; public String response; }
![Page 46: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/46.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4646
Custom Mapping• Property names• Property order• Ignoring properties• Null handling• Simple values
• Custom instantiation• Custom visibility• Adapters• Date/Number Formats• Binary Handling
![Page 47: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/47.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4747
Custom Mapping - Property Namespublic class Customer { public int id; public String firstName; }
{ "id": 1, "firstName": "Jason"}
![Page 48: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/48.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4848
Custom Mapping - Property Namespublic class Customer { private int id;
@JsonbProperty("name") private String firstName; }
public class Customer { public int id; public String firstName; @JsonbProperty("name") public String getFirstName() { return firstName; } }
{ "id": 1, "name": "Jason"}
![Page 49: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/49.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4949
Custom Mapping - Property Namespublic class Customer { public int id; public String firstName; @JsonbProperty(“getter-name") String getFirstName() { return firstName; } @JsonbProperty(“setter-name") void setFirstName(String str) { this.firstName = str; } }
Serialization:
{ "id": 1, “getter-name": "Jason"}
Deserialization:
{ "id": 1, “setter-name": "Jason"}
![Page 50: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/50.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5050
Custom Mapping - Property Naming Strategy• IDENTITY• LOWER_CASE_WITH_DASHES• LOWER_CASE_WITH_UNDERSCORES• UPPER_CAMEL_CASE• UPPER_CAMEL_CASE_WITH_SPACES• CASE_INSENSITIVE
myMixedCasePropertymy-mixed-case-propertymy_mixed_case_propertyMyMixedCasePropertyMy Mixed Case PropertymyMixedCasePropertymYmIxEdCaSePrOpErTy
JsonbConfig().withPropertyNamingStrategy(…)
![Page 51: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/51.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5151
Custom Mapping - Property Order Strategy• Strategies:
• LEXICOGRAPHICAL• ANY• REVERSE
• @JsonbPropertyOrder annotation on class• JsonbConfig().withPropertyOrderStrategy(…)
{ "a": 1, "b": 2, "c": 3 }
{ "b": 2, "a": 1, "c": 3 }
{ "c": 3, "b": 2, "a": 1 }
![Page 52: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/52.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5252
Custom Mapping - Ignoring Propertiespublic class Customer { public int id; public String name; }
public class Customer { public int id;
@JsonbTransient public String name; }
{ "id": 1, "name": "Jason"}
{ "id": 1 }
![Page 53: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/53.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5353
Custom Mapping - Null handlingpublic class Customer { private int id = 1; private String name = null; }
{ "id": 1 }
![Page 54: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/54.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5454
Custom Mapping - Null handlingpublic class Customer { private int id = 1; private String name = null; }
public class Customer { private int id = 1;
@JsonbNillable private String name = null; }
{ "id": 1 }
{ "id": 1, "name": null}
![Page 55: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/55.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5555
Custom Mapping - Null handlingpublic class Customer { private int id = 1; private String name = null; }
public class Customer { private int id = 1;
@JsonbNillable private String name = null; }
@JsonbNillable public class Customer { private int id = 1; private String name = null; }
{ "id": 1 }
{ "id": 1, "name": null}
{ "id": 1, "name": null}
![Page 56: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/56.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5656
Custom Mapping - Simple Valuepublic class Language { public int id = 1;
@JsonbValue public String code = “en";
public String name = "English"; }
”en”
![Page 57: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/57.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5757
Custom Mapping - Simple Valuepublic class Language { public int id = 1;
@JsonbValue public String code = “en";
public String name = "English"; }
public class Customer { public int id; public Language lang; public String name; }
”en”
{ "id": 1, "lang": ”en”, "name": ”Jason”}
![Page 58: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/58.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5858
Custom Mapping - Custom Instantiationpublic class Customer { public int id; public String name;
@JsonbCreator public static Customer getFromDb(int id) { return CustomerDao.getByPrimaryKey(id); } }
public class Order { public int id; public Customer customer; }
{ "id": 1, "customer": { "id": 2 } }
![Page 59: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/59.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5959
Custom Mapping - Custom Visibilitypublic class Customer { public int id; private String name; }
@JsonbVisibility(MyVisibilityStrategy.class) public class Customer { public int id; private String name; }
{ "id": 1 }
{ "id": 1, "name": "Jason" }
JsonbConfig config = new JsonbConfig() .withPropertyVisibilityStrategy(new MyVisibilityStrategy()); Jsonb jsonb = JsonbBuilder.create(config); String json = jsonb.toJson(obj);
![Page 60: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/60.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6060
Custom Mapping - Adapterspublic class Customer { public int id; public String name; @JsonbTypeAdapter(MyAdapter.class) public CustomerData data; }
{ "id": 1, "name": "Jason", "data": {…}, }
JsonbConfig config = new JsonbConfig().withAdapters(new MyAdapter()); Jsonb jsonb = JsonbBuilder.create(config); String json = jsonb.toJson(obj);
![Page 61: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/61.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6161
Custom Mapping - Date/Number Formatpublic class FormatTest { public Date defaultDate;
@JsonbDateFormat("dd.MM.yyyy") public Date formattedDate;
public BigDecimal defaultNumber;
@JsonbNumberFormat(“#0.00") public BigDecimal formattedNumber; }
{ “defaultDate”: “2015-07-26T23:00:00",
“formattedDate”: ”26.07.2015",
“defaultNumber": 1.2,
“formattedNumber": 1.20 }
![Page 62: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/62.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6262
Custom Mapping - Binary Data Encoding• BYTE (default)• BASE_64• BASE_64_URL
JsonbConfig config = new JsonbConfig() .withBinaryDataStrategy(BinaryDataStrategy.BASE_64);
Jsonb jsonb = JsonbBuilder.create(config); String json = jsonb.toJson(obj);
![Page 63: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/63.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6363
Custom Mapping - I-JSON• I-JSON (”Internet JSON”) is a restricted profile of JSON• JSON-B fully supports I-JSON by default with three exceptions• Configuration: withStrictIJSONSerializationCompliance
![Page 64: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/64.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What’s Next
64
![Page 65: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/65.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6565
In Progress• Early Draft 2 • Reference implementation started at http://eclipselink.org
![Page 66: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/66.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6666
TODO• Mapping subset of documents (JsonPointer) • Mapping 3rd party objects • Bi-directional mapping
![Page 67: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/67.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6767
Special Thanks• Martin Grebac, Martin Vojtek and all the experts • Kiev Java User Group (Olena Syrota, Oleg Tsal-Tsalko) • David Delabassee, Reza Rahman, Heather VanCura, John Clingan • others on [email protected]
![Page 68: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/68.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6868
Call to Action!• Participate!
[email protected]• Contribute!
http://eclipselink.org
![Page 69: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/69.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6969
Links• Spec Project:
https://java.net/projects/jsonb-spec/pages/Home• JCP Home:
https://www.jcp.org/en/jsr/detail?id=367• Reference Implementation:
http://eclipselink.org
![Page 70: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/70.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Q&A
70
![Page 71: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/71.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
71
![Page 72: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/72.jpg)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 72
![Page 73: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/73.jpg)
![Page 74: What’s new in JSR 367 Java API for JSON Binding](https://reader034.vdocument.in/reader034/viewer/2022051404/5871424e1a28ab55588b4bf3/html5/thumbnails/74.jpg)