Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
JSON ALAPOK
{
  "name": "John Doe",
  "age": 29,
  "siblings": ["Jane Doe", "Herry Doe"],
  "address": {
    "city": "Budapest",
    "...
{
  "name": "John Doe", // <­ String
  "age": 29, // <­ Number
  "siblings": ["Jane Doe", "Herry Doe"], // <­ Array
  "add...
PROGRAMMING MODELS
object model
streaming model
PACKAGE: JAVAX.JSON
reader interface
writer interface
model builder interface
PACKAGE: JAVAX.JSON.STREAM
parser interface
generator interface
LET'S CODE
Docs:
https://docs.oracle.com/javaee/7/api/javax/json/package-
summary.html
EXERCISE 1:
Olvassunk be json modelt stringből Használjunk hozzá
StringReader-t és JsonReader-t
Fordításhoz használjuk a G...
<!­­?xml version="1.0" encoding="UTF­8"?­­>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.
 ...
              </configuration>
          </plugin>
      </plugins>
  </build>
  <dependencies>
      <dependency>
       ...
import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import java.io.StringReader;
public...
EXERCISE 2:
Készítsünk json modelt builder segítségével és írjuk ki
stringbe Használjunk JsonObjectBuilder-t,
JsonArrayBui...
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import java.io.StringWriter;
public cl...
            .add("type", "home")
            .add("number", "222­222­2222")))
            .build();
        StringWriter s...
EXERCISE 3:
Parsoljunk json stringet és irassuk ki a parse eventeket
Használjunk JsonParser-t
import javax.json.Json;
import javax.json.stream.JsonParser;
import java.io.StringReader;
public class Exercise03 {
    pr...
                    System.out.println(event.toString());
                    break;
                case KEY_NAME:
      ...
EXERCISE 4:
Készítsünk JSON stringet geerátor segítségével Használjunk
JsonGenerator-t, StringWriter-t
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import java.io.StringWriter;
public class Exercise04 {
   ...
            .writeEnd()
            .writeStartObject()
            .write("type", "home")
            .write("number", "2...
JSR374 (JSON PROCESSING 1.1)
wget http://download.oracle.com/otn-pub/jcp/json_p-
1_1-edr-spec/jsonp-1.1-edr1-sources.zip
w...
RESOURCES:
https://docs.oracle.com/javaee/7/api/javax/json/package-
summary.html
https://docs.oracle.com/javaee/7/tutorial...
Upcoming SlideShare
Loading in …5
×

Jsonp coding dojo

154 views

Published on

This a Coding Dojo prepared for the HOUG (http://www.houg.hu/) Szakmai nap 2016

Published in: Software
  • Be the first to comment

  • Be the first to like this

Jsonp coding dojo

  1. 1. JSON ALAPOK
  2. 2. {   "name": "John Doe",   "age": 29,   "siblings": ["Jane Doe", "Herry Doe"],   "address": {     "city": "Budapest",     "street": "Deak Square"   },   "alive": false,   "description": null }
  3. 3. {   "name": "John Doe", // <­ String   "age": 29, // <­ Number   "siblings": ["Jane Doe", "Herry Doe"], // <­ Array   "address": {       "city": "Budapest",       "street": "Deak Square"   }, // <­ Object   "alive": false, // <­ true/false   "description": null // <­ null }
  4. 4. PROGRAMMING MODELS object model streaming model
  5. 5. PACKAGE: JAVAX.JSON reader interface writer interface model builder interface
  6. 6. PACKAGE: JAVAX.JSON.STREAM parser interface generator interface
  7. 7. LET'S CODE
  8. 8. Docs: https://docs.oracle.com/javaee/7/api/javax/json/package- summary.html
  9. 9. EXERCISE 1: Olvassunk be json modelt stringből Használjunk hozzá StringReader-t és JsonReader-t Fordításhoz használjuk a Glassfish implementációt: org.glassfish; javax.json; 1.0.4 http://mvnrepository.com/artifact/org.glassfish/javax.json/1.0.4
  10. 10. <!­­?xml version="1.0" encoding="UTF­8"?­­> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.   <modelversion>4.0.0</modelversion>   <groupid>hu.huog.jsonp</groupid>   <artifactid>exercises</artifactid>   <version>1.0­SNAPSHOT</version>   <packaging>jar</packaging>   <build>       <plugins>           <plugin>               <groupid>org.apache.maven.plugins</groupid>               <artifactid>maven­jar­plugin</artifactid>               <configuration>                   <archive>                       <manifest>                           <mainclass>Exercise00</mainclass>                           <addclasspath>true</addclasspath>                       </manifest>                   </archive>
  11. 11.               </configuration>           </plugin>       </plugins>   </build>   <dependencies>       <dependency>           <groupid>org.glassfish</groupid>           <artifactid>javax.json</artifactid>           <version>1.0.4</version>       </dependency>   </dependencies> </project>           
  12. 12. import javax.json.Json; import javax.json.JsonReader; import javax.json.JsonStructure; import java.io.StringReader; public class Exercise01 {     private static final String jsonString = "{ "name": "John Snow"}"     public static void main(String[] args) {         JsonReader reader = Json.createReader(new StringReader(jsonString));         JsonStructure jsonst = reader.read();         reader.close();         System.out.println("json: " + jsonst);     } }           
  13. 13. EXERCISE 2: Készítsünk json modelt builder segítségével és írjuk ki stringbe Használjunk JsonObjectBuilder-t, JsonArrayBuilder-t, JsonWriter-t, StringWriter-t
  14. 14. import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonWriter; import java.io.StringWriter; public class Exercise01 {     public static void main(String[] args) {         JsonObject model = Json.createObjectBuilder()             .add("firstName", "Duke")             .add("lastName", "Java")             .add("age", 18)             .add("streetAddress", "100 Internet Dr")             .add("city", "JavaTown")             .add("state", "JA")             .add("postalCode", "12345")             .add("phoneNumbers", Json.createArrayBuilder()             .add(Json.createObjectBuilder()             .add("type", "mobile")             .add("number", "111­111­1111"))             .add(Json.createObjectBuilder()
  15. 15.             .add("type", "home")             .add("number", "222­222­2222")))             .build();         StringWriter strWriter = new StringWriter();         JsonWriter jsonWriter = Json.createWriter(strWriter);         jsonWriter.writeObject(model);         jsonWriter.close();         String jsonStr = strWriter.toString();         System.out.println("json: " + jsonStr);     } }           
  16. 16. EXERCISE 3: Parsoljunk json stringet és irassuk ki a parse eventeket Használjunk JsonParser-t
  17. 17. import javax.json.Json; import javax.json.stream.JsonParser; import java.io.StringReader; public class Exercise03 {     private static final String jsonString = "{ "name": "John Snow"}"     public static void main(String[] args) {         JsonParser parser = Json.createParser(new StringReader(jsonString));         while (parser.hasNext()) {             JsonParser.Event event = parser.next();             switch(event) {                 case START_ARRAY:                 case END_ARRAY:                 case START_OBJECT:                 case END_OBJECT:                 case VALUE_FALSE:                 case VALUE_NULL:                 case VALUE_TRUE:
  18. 18.                     System.out.println(event.toString());                     break;                 case KEY_NAME:                     System.out.print(event.toString() + " " +                     parser.getString() + " ­ ");                     break;                 case VALUE_STRING:                 case VALUE_NUMBER:                     System.out.println(event.toString() + " " +                     parser.getString());                     break;             }         }     } }           
  19. 19. EXERCISE 4: Készítsünk JSON stringet geerátor segítségével Használjunk JsonGenerator-t, StringWriter-t
  20. 20. import javax.json.Json; import javax.json.stream.JsonGenerator; import java.io.StringWriter; public class Exercise04 {     public static void main(String[] args) {         StringWriter writer = new StringWriter();         JsonGenerator gen = Json.createGenerator(writer);         gen.writeStartObject()             .write("firstName", "Duke")             .write("lastName", "Java")             .write("age", 18)             .write("streetAddress", "100 Internet Dr")             .write("city", "JavaTown")             .write("state", "JA")             .write("postalCode", "12345")             .writeStartArray("phoneNumbers")             .writeStartObject()             .write("type", "mobile")             .write("number", "111­111­1111")
  21. 21.             .writeEnd()             .writeStartObject()             .write("type", "home")             .write("number", "222­222­2222")             .writeEnd()             .writeEnd()             .writeEnd();         gen.close();         String jsonString = writer.toString();         System.out.println("json:" + jsonString);     } }           
  22. 22. JSR374 (JSON PROCESSING 1.1) wget http://download.oracle.com/otn-pub/jcp/json_p- 1_1-edr-spec/jsonp-1.1-edr1-sources.zip wget http://download.oracle.com/otn-pub/jcp/json_p- 1_1-edr-spec/jsonp-1.1-edr1-javadoc.zip http://download.oracle.com/otndocs/jcp/json_p-1_1-edr- spec/index.html
  23. 23. RESOURCES: https://docs.oracle.com/javaee/7/api/javax/json/package- summary.html https://docs.oracle.com/javaee/7/tutorial/jsonp.htm http://www.jcp.org/en/jsr/detail?id=353 http://www.jcp.org/en/jsr/detail?id=374 https://github.com/google/gson https://github.com/FasterXML/jackson

×