SlideShare a Scribd company logo
JSON ALAPOK
{
  "name": "John Doe",
  "age": 29,
  "siblings": ["Jane Doe", "Herry Doe"],
  "address": {
    "city": "Budapest",
    "street": "Deak Square"
  },
  "alive": false,
  "description": null
}
{
  "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
}
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 Glassfish implementációt:
org.glassfish; javax.json; 1.0.4
http://mvnrepository.com/artifact/org.glassfish/javax.json/1.0.4
<!­­?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>
              </configuration>
          </plugin>
      </plugins>
  </build>
  <dependencies>
      <dependency>
          <groupid>org.glassfish</groupid>
          <artifactid>javax.json</artifactid>
          <version>1.0.4</version>
      </dependency>
  </dependencies>
</project>
          
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);
    }
}
          
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
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()
            .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);
    }
}
          
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 {
    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:
                    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;
            }
        }
    }
}
          
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 {
    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")
            .writeEnd()
            .writeStartObject()
            .write("type", "home")
            .write("number", "222­222­2222")
            .writeEnd()
            .writeEnd()
            .writeEnd();
        gen.close();
        String jsonString = writer.toString();
        System.out.println("json:" + jsonString);
    }
}
          
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
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

More Related Content

PPTX
Introduction to Go
PDF
Full stack security
PDF
Scaling on AWS
PDF
Microservices and modularity with java
PDF
Garbage First Garbage Collector Algorithm
PDF
Power tools in Java
PDF
Két Java fejlesztő első Scala projektje
PDF
Server in your Client
Introduction to Go
Full stack security
Scaling on AWS
Microservices and modularity with java
Garbage First Garbage Collector Algorithm
Power tools in Java
Két Java fejlesztő első Scala projektje
Server in your Client

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PPTX
assetexplorer- product-overview - presentation
PDF
Cost to Outsource Software Development in 2025
PDF
Download FL Studio Crack Latest version 2025 ?
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
AutoCAD Professional Crack 2025 With License Key
Designing Intelligence for the Shop Floor.pdf
Monitoring Stack: Grafana, Loki & Promtail
Operating system designcfffgfgggggggvggggggggg
17 Powerful Integrations Your Next-Gen MLM Software Needs
Salesforce Agentforce AI Implementation.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Complete Guide to Website Development in Malaysia for SMEs
wealthsignaloriginal-com-DS-text-... (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
assetexplorer- product-overview - presentation
Cost to Outsource Software Development in 2025
Download FL Studio Crack Latest version 2025 ?
Ad
Ad

Jsonp coding dojo