java api for json binding - introduction and update

83

Upload: martin-grebac

Post on 28-Nov-2014

191 views

Category:

Software


7 download

DESCRIPTION

Slides from JavaOne 2014 Session Java API for JSON Binding - Introduction and update.

TRANSCRIPT

Page 1: Java API for JSON Binding - Introduction and update
Page 2: Java API for JSON Binding - Introduction and update
Page 3: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Java  API  for  JSON  BindingIntroduction  and  update

Martin  Grebac  JSON-­‐B  Spec  lead  Development  Manager  TopLink  &  Metro  Oracle

Accept:  application/json

Page 4: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Program  Agenda

Introduction  

JSR;  Status;  Next  Steps  

API  

Integration  

Q  &  A

1

2

3

4

5

4

Page 5: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  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.

5

Page 6: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Introduction

6

Page 7: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

7

Page 8: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

8

public  class  Employee  {                  public  int  id;                  public  String  firstName;                  public  String  lastName;                  public  String  email;                  ….  }

Page 9: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

9

!Employee  e  =  new  Employee();  e.id  =  1111;  e.firstName  =  “Martin”;  e.lastName  =  “Grebac”;  e.email  =  “[email protected]”;

Page 10: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

9

!Employee  e  =  new  Employee();  e.id  =  1111;  e.firstName  =  “Martin”;  e.lastName  =  “Grebac”;  e.email  =  “[email protected]”;

Page 11: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

9

!Employee  e  =  new  Employee();  e.id  =  1111;  e.firstName  =  “Martin”;  e.lastName  =  “Grebac”;  e.email  =  “[email protected]”;

{          "id":  1111,          "firstName"  :  "Martin",          "lastName"  :  "Grebac",          "email":  "[email protected]"  }

Page 12: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

What  is  JSON  Binding?

• Converting  Java  objects  to  &  from  JSON

10

JAX-­‐RS

Objects

XML

JSON

JAXB

JSON-­‐B

Page 13: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Standardizing  current  technology

• Many  mature  or  emerging  JSON  binding  frameworks  out  there

11

!• Jackson  • Genson  • Gson  • EclipseLink  

MOXy  • Fleece  • JSON-­‐lib  • Flexjson  • Json-­‐io  • JSONiJ  • Xstream  • …

Page 14: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Standardizing  current  technology

• Many  mature  or  emerging  JSON  binding  frameworks  out  there

11

!• Jackson  • Genson  • Gson  • EclipseLink  

MOXy  • Fleece  • JSON-­‐lib  • Flexjson  • Json-­‐io  • JSONiJ  • Xstream  • …

Page 15: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Standardizing  current  technology

• Many  mature  or  emerging  JSON  binding  frameworks  out  there

11

!• Jackson  • Genson  • Gson  • EclipseLink  

MOXy  • Fleece  • JSON-­‐lib  • Flexjson  • Json-­‐io  • JSONiJ  • Xstream  • …

Page 16: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Standard  API  -­‐  Advantages

• Switch  providers  easily  • Implementation  compete  on  common  ground  (performance  or  feature-­‐wise)  • Loss  of  implementation/participant  is  no  issue  for  standard  • More  people  able  to  help  on  forums  or  stackoverflow  ;)  even  those  not  using  your  implementation  of  choice

12

Page 17: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    13

Page 18: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

JSR;  Status;  Next  Steps

14

Page 19: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Status

• JSR  Proposal  Submitted  • https://www.jcp.org/en/jsr/detail?id=367

JSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

15

Page 20: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    16

Proposal  –  In  short• API  with  similar  feel  to  JAXB  !• javax.json.bind  !• Spec  project:  http://jsonb-­‐spec.java.net  (stay  tuned)  !• RI  project:  http://eclipselink.org  !• Standalone,  looking  for  Java  EE  8

Page 21: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Status

• JSR  Proposal  Submitted  • https://www.jcp.org/en/jsr/detail?id=367  

• JSR  Review  period  finished  08/Sep/2014  • JSR  Review  ballot  finished  22/Sep/2014

JSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

17

Page 22: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Status

• JSR  Proposal  Submitted  • https://www.jcp.org/en/jsr/detail?id=367  

• JSR  Review  period  finished  08/Sep/2014  • JSR  Review  ballot  finished  22/Sep/2014

JSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

17

Page 23: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Status

• JSR  Proposal  Submitted  • https://www.jcp.org/en/jsr/detail?id=367  

• JSR  Review  period  finished  08/Sep/2014  • JSR  Review  ballot  finished  22/Sep/2014  !• Accepting  EG  nominations:  

• https://www.jcp.org/en/jsr/egnom?id=367

JSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

18

Page 24: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Next  StepsJSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

19

• Form  Expert  Group  • Early  Draft

Page 25: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Next  StepsJSR  367  –  Java  API  for  JSON  Binding  (JSON-­‐B)

20

• Form  Expert  Group  • Early  Draft

Page 26: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

API  –  Goals,  Non-­‐goals  -­‐  Proposal

21

Page 27: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    22

Proposal  –  Goals• Binding  JSON  (RFC  7159)  documents  •Maintain  consistency  with  JAXB  API  and  other  Java  SE  &  EE  APIs  where  applicable  • Define  default  object-­‐JSON  mapping  • Annotation  based  customizations  for  object-­‐JSON  mapping  • Support  binding  using  JSON-­‐P  APIs  !• Explore  mapping  subset  of  Json  documents  (Json  Pointer)  • Explore  options  to  map  3rd  party  objects  • Explore  bi-­‐directional  mappings

Page 28: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    23

Proposal  –  Non-­‐goals• JSON  Schema  • Tooltime  • Roundtrip  • JEP  198  -­‐  Lightweight  JSON  API

Page 29: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Runtime  API  -­‐  ProposalAnything  subject  to  change  anytime

24

Page 30: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    25

import  javax.json.bind.*;  !public  void  init()  {  !

JsonContext  context  =  JsonContext.newInstance();  !//  JsonContext  context  =    //            JsonContext.newInstance(“com.provider.ProviderFactory");  

!Marshaller  marshaller  =  context.createMarshaller();  !Unmarshaller  unmarshaller  =  context.createUnmarshaller;  !

}

Proposal  –  Runtime  Initialization  –  Default

Page 31: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    26

public  void  init()  {  !

JsonContext  context  =  new  JsonContextBuilder()               .setScope  (MyClass.class,  …)           .excludeField(“field”,  MyClass.class)           .addFilter(myContextFilterImplementation)           .build();  

}

Proposal  –  Runtime  Initialization  –  Context  Builder

Page 32: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    27

public  void  init()  {  !JsonContext  context     =  JsonContext.newInstance();  !Marshaller  marshaller     =  context.createMarshaller()                       .setProperty(Marshaller.PROPERTY)                 .setPrettyPrinting(true);                  !Unmarshaller  unmarshaller  =  context.createUnmarshaller()             .setCloseStreams(true);  !

}

Proposal  –  Runtime  Initialization  –  Fluent

Page 33: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    28

public  void  marshal()  {  !!

Marshaller  marshaller  =     jsonContext.createMarshaller();  !//to  String  String  string  =  marshaller.marshal(myObject);  !//to  Writer  marshaller.marshal(myObject,  new  FileWriter(“file.json”));  !!

}

Proposal  –  Marshalling  (Writing)

Page 34: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    29

public  void  marshal()  {  !

Marshaller  marshaller  =  jsonContext.createMarshaller();  !//to  JSONP  JsonGenerator  FileWriter  writer  =  new  FileWriter(“file.json”);  JsonGenerator  generator  =  Json.createGenerator(writer);  marshaller.marshal(myObject,  generator);  !//expose  as  JSONP  JsonParser  JsonParser  parser  =  marshaller.getJsonParser(myObject);  !//other  JSONP  constructs  JsonObjectBuilder,  …  !

}

Proposal  –  Marshalling  (Writing)

Page 35: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    30

public  void  unmarshal()  {  !

Unmarshaller  unmarshaller  =  jsonContext.createUnmarshaller();  !//from  String  String  s  =  "{\"field\":\"Value\"}";    MyClass  myInstance1  =  (MyClass)  unmarshaller.unmarshal(s);  MyClass  myInstance2  =  unmarshaller.unmarshal(s,  MyClass.class);  !//from  Reader  unmarshaller.unmarshal(new  FileReader("file.json"));  !

}

Proposal  –  Unmarshalling  (Reading)

Page 36: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    31

public  void  unmarshal()  {  !

Unmarshaller  unmarshaller  =  jsonContext.createUnmarshaller();  !//from  JSONP  JsonParser  File  stream  =  new  FileInputStream(“file.json”);  JsonParser  jsonParser  =  Json.createParser(stream);    !MyClass  myInstance  =  unmarshaller.unmarshal(jsonParser);  !//other  JSONP  constructs  …  JsonStructure,  JsonObject,  JsonArray  …  !

}

Proposal  –  Unmarshalling  (Reading)

Page 37: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    32

public  void  unmarshal()  {  !

Unmarshaller  u  =  jsonContext.createUnmarshaller();  !//expose  JSONP  JsonGenerator  UnmarshallerJsonGenerator  generator  =  u.getJsonGenerator();  generator.write(…);  generator.write(…);  …  MyClass  myInstance  =  (MyClass)  generator.getResult();  !

}

Proposal  –  Unmarshalling  (Reading)

Page 38: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    33

!String  myString  =  Jsonb.marshal(myObject);  !MyObject  object  =  Jsonb.unmarshal(someJsonString,  MyObject.class);  !…  !

Proposal  –  The  Shortcut  -­‐  Jsonb

Page 39: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Mapping  API  -­‐  Proposal

34

Page 40: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    35

Proposal  –  Default  Mapping  –  Primitive  types

char  _char;   “_char"      :  "c",  String  _string;   "_string"  :  “string”,  !byte  _byte;   "_byte"    :  123,    short  _short;   "_short"  :  32767,  int  _int;     "_int"      :  2147483647,  long  _long;   "_long"    :  9223372036854775807,            float  _float;   "_float"    :  3.4028235E38,  double  _double;   "_double"  :  1.7976931348623157E308,  !boolean  _boolean;   "_boolean"  :  true,  Boolean  _Boolean;   "_Boolean"  :  false,            Byte  _Byte;   “_Byte"        :  127,  Short  _Short;           "_Short"      :  32767,  Integer  _Integer;   "_Integer"  :  2147483647,  Long  _Long;   "_Long"        :  9223372036854775807,

Float  _Float;   "_Float"  :  3.4028235E38,  Double  _Double;   "_Double"  :  1.7976931348623157E308,  !//defaults  -­‐  Jackson:  "NaN",  Moxy:  NaN  //Genson  throws  exception  …  Float  _NaNFloat  =  Float.NaN;  !//same  as  above  Double  _NaNDouble  =  Double.NaN;            BigInteger  _BigInteger;  "_BigInteger"  :  4611686014132,  BigDecimal  _BigDecimal;  "_BigDecimal"  :  850705917302332501249,  !String[]  _stringArr;    “_stringArr"  :  [  "abc",  "def",  "ghi"  ],  int[]  _intArray;        "_intArr"        :  [  123,  456,  789  ],  !String[]  _stringArrSingle;    "_stringArrSingle"  :  [  "abc"  ],  int[]  _intArrSingle;    "_intArrSingle"        :  [  123  ]

Page 41: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    36

Proposal  –  Default  Mapping  –  Dates

• Date  • Calendar  • JDK  8  Date&Time  API  !• Adapter  /  JsonDe/serializer,  …  • JsonFormat  annotation  • .useDateFormat(new  SimpleDateFormat(…))  • .setDateFormat(new  SimpleDateFormat(…))

Page 42: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    37

Proposal  –  Default  Mapping  -­‐  Dates  -­‐  JsonAdapter

public  class  DateAdapter  implements  JsonAdapter<Date>  {        public  String  marshal(Date  d)  {…}        public  Date  unmarshal(String  s)  {…}  }  !public  class  Parent  {          @JsonJavaTypeAdapter(DateAdapter.class)          public  Date  date;  }

Page 43: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    38

Proposal  –  Default  Mapping  -­‐  Enum

@JsonEnum  public  enum  Options  {          @JsonEnumValue(“1”)  OPTION_1,          @JsonEnumValue(“2”)  OPTION_2,          @JsonEnumValue(“3”)  OPTION_3;  }  

{      “option”  :  1  }  !!!!!{      “option”  :  1  }

option  :  OPTION_1

Page 44: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    39

Proposal  –  Null  value  handling  -­‐  Default

public  String  longDescription;  !

{  }  !!!!!!!{  }

longDescription  :  null  (do  not  redefine)

Page 45: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    40

Proposal  –  Null  value  handling  -­‐  JsonNillable

@JsonNillable(true)  public  String  longDescription;  !

{      “longDescription”  :  null  }  !!!!!{      “longDescription”  :  null  }

longDescription  :  null

Page 46: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    41

Proposal  –  Customized  Mapping  -­‐  Names

@JsonProperty(“longdesc”);  public  String  longDescription;  !

{      “longdesc”  :  “This  is  long  description”,  }  !!!!!{      “longdesc”  :  “This  is  long  description”,  }

longDescription  :  “This  is  long  description”

Page 47: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    42

Proposal  –  Customized  Mapping  -­‐  Names

private  String  longDescription;  !@JsonProperty(“long-­‐desc”);  public  String  getLongDescription()  {  

return  longDescription;  }  !@JsonProperty(“longDesc”);  public  void  setLongDescription(String  ld)  {      this.longDescription  =  ld;  }  

{      “longDesc”  :  “This  is  long  description”,  }  !!!!!{      “long-­‐desc”  :  “This  is  long  description”,  }

longDescription  :  “This  is  long  description”

Page 48: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    43

Proposal  –  Customized  Mapping  –  Ignoring  fields,properties

@JsonTransient  public  String  longDescription;  

{      “longDesc”  :  “This  is  long  description”,  }  !!!!!!{  }

Page 49: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    44

Proposal  –  Customized  Mapping  –  Ignoring  fields,properties

@JsonTransient  public  String  longDescription;  

!!!!!!!!!{  }

longDescription  :  “This  is  not  the  long  description  from  json  document”

Page 50: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    45

Proposal  –  Customized  Mapping  –  Ignoring  fields,properties

private  String  longDescription;  !@JsonTransient  public  String  getLongDescription()  {  

return  longDescription;  }  !public  void  setLongDescription(String  ld)  {      this.longDescription  =  ld;  }

{      “longDesc”  :  “This  is  long  description”,  }  !!!!!{        }

longDescription  :  “This  is  long  description”

Page 51: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    46

Proposal  –  Customized  Mapping  –  Ignoring  fields,properties

private  String  longDescription;  !public  String  getLongDescription()  {  

return  longDescription;  }  !@JsonTransient  public  void  setLongDescription(String  ld)  {      this.longDescription  =  ld;  }

{      “longDesc”  :  “This  is  long  description”  }  !!!!!{      “longDesc”  :  “Different  description”  }

longDescription  :  “Different  description”

Page 52: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    47

Proposal  –  Customized  Mapping  –  Ordering

@JsonPropertyOrder(“title”,  “author”)  public  class  Employee  {          public  String  author;          public  String  title;  }  

!!!!!!!{      “title”  :  “Effective  Java”,      “author”  :  “Joshua  Bloch”  }

Book:      :  author      :  title

Page 53: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    48

Proposal  –  Customized  Mapping  –  Access

@JsonAccessorType(JsonAccessType.FIELD)  public  class  Employee  {          private  String  author;          private  String  title;  }  !NONE  FIELD  PUBLIC  PROTECTED_PUBLIC  ANY  …  ?

Page 54: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    49

Proposal  –  Simple  value  mapping

public  class  Description  {                    private  String  longDescription;  !        @JsonValue          public  String  getLongDescription()  {  

       return  longDescription;          }  !        public  void  setLongDescription(String  ld)  {                this.longDescription  =  ld;          }  }

!!!!!!!!{      “title”  :  “Title”,      “author”  :  “Author  Name”,      “description”  :  “This  is  long  description”,      “summary”  :  “Summary”,      }

Description:    :  longDescription  =  “This  is  long  description”

Book:    :  title    :  author    :  longDescription    :  summary

Page 55: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Need  joke  to  cool  down  …  

50

Page 56: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

@mgrebac

51

Page 57: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    52

http://www.tehnrd.com/

Page 58: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    53

http://www.tehnrd.com/

• Hear  someone  talking  about  how  cool,  efficient,  and  great  to  work  with  Jason  is  …  

Page 59: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    54

http://www.tehnrd.com/

• Hear  someone  talking  about  how  cool,  efficient,  and  great  to  work  with  Jason  is  …    !• …  and  then  realizing  they  are  talking  about  JSON!  

Page 60: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    55

Proposal  -­‐  Inheritance,  Polymorphism@JsonDiscriminatorProperty(“type”)  public  abstract  class  ContactInfo  {}  !!public  class  Address  extends  ContactInfo  {      public  String  street;  }  !!public  class  Email  extends  ContactInfo  {      public  String  email;  }  !!public  class  Phone  extends  ContactInfo  {      public  String  phoneNumber;  }  !

Page 61: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    56

Proposal  -­‐  Inheritance,  Polymorphism@JsonDiscriminatorProperty(“type”)  public  abstract  class  ContactInfo  {}  !@JsonDiscriminatorValue(“address-­‐type”)  public  class  Address  extends  ContactInfo  {      public  String  street;  }  !@JsonDiscriminatorValue(“email-­‐type”)  public  class  Email  extends  ContactInfo  {      public  String  email;  }  !@JsonDiscriminatorValue(“phone-­‐type”)  public  class  Phone  extends  ContactInfo  {      public  String  phoneNumber;  }  !

Page 62: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    56

Proposal  -­‐  Inheritance,  Polymorphism@JsonDiscriminatorProperty(“type”)  public  abstract  class  ContactInfo  {}  !@JsonDiscriminatorValue(“address-­‐type”)  public  class  Address  extends  ContactInfo  {      public  String  street;  }  !@JsonDiscriminatorValue(“email-­‐type”)  public  class  Email  extends  ContactInfo  {      public  String  email;  }  !@JsonDiscriminatorValue(“phone-­‐type”)  public  class  Phone  extends  ContactInfo  {      public  String  phoneNumber;  }  !

!{      “type”  :  “address-­‐type”,      “street”  :  “1099  Lombard  Street”  }  !!!!{      “type”  :  “email-­‐type”,      “email”  :  “[email protected]”  }  

Address

Email

Page 63: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    57

Proposal  –  Inheritance,  Polymorphism  supporting@JsonSeeAlso(          {Address.class,  Phone.class,  Email.class}  )  @JsonDiscriminatorProperty(“type”)  public  abstract  class  ContactInfo  {}

Page 64: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    58

Proposal  –  Instantiation  –  Factories,  Constructors

public  class  CreatedClass  {          @JsonCreator          public  static  CreatedClass  create()  {                  return  new  CreatedClass();          }  }  !public  class  ConstrClass  {          public  ConstrClass(                                        @JsonProperty(“name”)  name,                                        @JsonProperty(“email”)  email)  {                …          }  }                                                

{      “created”  :  {        },      “constructed”  :  {            “email”  :  “[email protected]”,            “name”  :  “Larry  Ellison”      }  }

ParentClass  {        //  by  CreatedClass.create()        CreatedClass  created;  !      /*  by  new  ConstrClass(                        “[email protected]”,                        “Larry  Ellison”              )        */        ConstructedClass  constructed;    }

Page 65: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    59

Proposal  –  Lifecycle  events

@JsonPreUnmarshal  @JsonPostUnmarshal  !@JsonPreMarshal  @JsonPostMarshal  

Page 66: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    60

Explore  mapping  3rd  party  objects• Mix-­‐in  annotations  (Jackson)  • External  metadata  format  (MOXy)  • …

Page 67: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    61

Explore  binding  JSON  document  subset  -­‐  JSONPointer  ?{        "title":  "Recent  Uploads  tagged  javaone",      "link":  "http://www.flickr.com/photos/tags/javaone/",      "description":  "",      "modified":  "2014-­‐03-­‐22T16:40:19Z",      "generator":  "http://www.flickr.com/",      "items":  [    {     "title":  "IMG_1377",     "link":  "http://www.flickr.com/photos/soniaauv/13332491484/",     "media":  {"m":"http://farm4.staticflickr.com/3788/13332491484_a96c2599e8_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T12:33:19-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:19Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:outdoor=0983  vision:plant=0872"        },  {     "title":  "IMG_1388",     "link":  "http://www.flickr.com/photos/soniaauv/13332247843/",     "media":  {"m":"http://farm3.staticflickr.com/2824/13332247843_f5805b59fd_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T21:45:56-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:26Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:mountain=0853  vision:sky=0722"        },        …

{

public  class  FlickrExtract  {  !        @JsonPointer(“/link”)          public  String  linkUrl;              @JsonPointer(“/items/0/title”)          public  String  firstPictureTitle;  !}

Page 68: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    62

Explore  binding  JSON  document  subset  -­‐  JSONPointer  ?{        "title":  "Recent  Uploads  tagged  javaone",      "link":  "http://www.flickr.com/photos/tags/javaone/",      "description":  "",      "modified":  "2014-­‐03-­‐22T16:40:19Z",      "generator":  "http://www.flickr.com/",      "items":  [    {     "title":  "IMG_1377",     "link":  "http://www.flickr.com/photos/soniaauv/13332491484/",     "media":  {"m":"http://farm4.staticflickr.com/3788/13332491484_a96c2599e8_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T12:33:19-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:19Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:outdoor=0983  vision:plant=0872"        },  {     "title":  "IMG_1388",     "link":  "http://www.flickr.com/photos/soniaauv/13332247843/",     "media":  {"m":"http://farm3.staticflickr.com/2824/13332247843_f5805b59fd_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T21:45:56-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:26Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:mountain=0853  vision:sky=0722"        },        …

{

public  class  FlickrExtract  {  !        @JsonPointer(“/items/0/media”)          public  Media  media;  !}  !public  class  Media  {          public  String  m;  }  

Page 69: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    63

Explore  binding  JSON  document  subset  -­‐  JSONPath  ?{        "title":  "Recent  Uploads  tagged  javaone",      "link":  "http://www.flickr.com/photos/tags/javaone/",      "description":  "",      "modified":  "2014-­‐03-­‐22T16:40:19Z",      "generator":  "http://www.flickr.com/",      "items":  [    {     "title":  "IMG_1377",     "link":  "http://www.flickr.com/photos/soniaauv/13332491484/",     "media":  {"m":"http://farm4.staticflickr.com/3788/13332491484_a96c2599e8_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T12:33:19-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:19Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:outdoor=0983  vision:plant=0872"        },  {     "title":  "IMG_1388",     "link":  "http://www.flickr.com/photos/soniaauv/13332247843/",     "media":  {"m":"http://farm3.staticflickr.com/2824/13332247843_f5805b59fd_m.jpg"},     "date_taken":  "2007-­‐05-­‐07T21:45:56-­‐08:00",     "description":  "  <p>…<\/p>  ",     "published":  "2014-­‐03-­‐22T16:40:26Z",     "author":  "[email protected]  (S.O.N.I.A.  ETS)",     "author_id":  "117446064@N05",     "tags":  "javaone  preparations  2007  vision:mountain=0853  vision:sky=0722"        },        …

{

public  class  FlickrExtract  {  !        @JsonPath(“$.link”)          public  String  linkUrl;              @JsonPath(“$.items.*.title”)          public  String[]  pictureTitles;  !}

Page 70: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Integration

64

Page 71: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Affected,  used,  under  consideration  specifications

• JSR  353  –  Java  API  for  JSON  Processing  (JSON-­‐P)  • JSR  339  –  Java  API  for  RESTful  services  (JAX-­‐RS)  • JSR  349  –  Bean  Validation  (BV)  • JSR  ???  –  JEP  198  –  Light-­‐Weight  JSON  API

65

Page 72: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

JAX-­‐RS

• JAX-­‐RS  allowed  to  standardize  on  JSON-­‐Binding  • Custom  JSON-­‐Binding

66

Page 73: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

JSON-­‐P

• Already  covered

67

Page 74: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

JPA

• Explore  mapping  bidirectional  references

Back-­‐reference  (Inverse  reference)  support

68

public  class  A  {          @JsonManagedReference          public  B  getB();  }  !public  class  B  {          @JsonBackReference          public  A  getA();  }

public  class  A  {          public  B  getB();  }  !!public  class  B  {          @XmlInverseReference(mappedBy=“b”)          public  A  getA();  }

Page 75: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Call  to  ACTION!• Participate  as  an  observer  

• users@jsonb-­‐spec.java.net  • Join  EG  

• https://jcp.org/en/jsr/detail?id=367

69

Page 76: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Q  &  A• Participate  as  an  observer  

• users@jsonb-­‐spec.java.net  • Join  EG  

• https://jcp.org/en/jsr/detail?id=367

70

Page 77: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Related  Talk(s)

• CON5742  -­‐  JSON  Pointer  and  JSON  Patch:  Updates  to  the  Java  API  for  JSON  Processing  • Kin-­‐Man  Chung  • Tuesday,  September  30,  4-­‐5pm,  Cyril  Magnin  II/III

71

Page 78: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

Sources,  References

• https://jcp.org/en/jsr/detail?id=367  • http://www.developer.com/lang/jscript/top-­‐7-­‐open-­‐source-­‐json-­‐binding-­‐providers-­‐available-­‐today.html  • http://goessner.net/articles/JsonPath/  • http://eclipselink.org

72

Page 79: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    

My  JSON  joke  fell  flat  because  I  got  the  quotes  wrong.

– Jeff  Waugh’s  tweet

73

Page 80: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  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.

74

Page 81: Java API for JSON Binding - Introduction and update

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    75

Page 82: Java API for JSON Binding - Introduction and update
Page 83: Java API for JSON Binding - Introduction and update