Package javax.json.stream

Examples of javax.json.stream.JsonParser


   @Test
   public void testJsonRead()
   {
      String jsonData = "{\"firstName\": \"George\", \"lastName\": \"Gastaldi\"}";
      JsonParser parser = Json.createParser(new StringReader(jsonData));
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.START_OBJECT, parser.next());
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.KEY_NAME, parser.next());
      Assert.assertEquals("firstName", parser.getString());
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.VALUE_STRING, parser.next());
      Assert.assertEquals("George", parser.getString());
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.KEY_NAME, parser.next());
      Assert.assertEquals("lastName", parser.getString());
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.VALUE_STRING, parser.next());
      Assert.assertEquals("Gastaldi", parser.getString());
      Assert.assertTrue(parser.hasNext());
      Assert.assertEquals(Event.END_OBJECT, parser.next());
      Assert.assertFalse(parser.hasNext());
   }
View Full Code Here


        generator.close();
        return writer.toString();
    }

    public static JobExecution unmarshall(final String json) {
        final JsonParser parser = Json.createParser(new StringReader(json));
        final JobExecutionBuilder builder = JobExecutionBuilder.create();

        String key = null;
        while (parser.hasNext()) {
            final Event event = parser.next();
            switch (event) {
                case KEY_NAME:
                    key = parser.getString();
                    break;
                case VALUE_FALSE:
                case VALUE_NULL:
                case VALUE_NUMBER:
                case VALUE_STRING:
                case VALUE_TRUE:
                    final String value = parser.getString();
                    if (key == null) {
                        throw new IllegalStateException(String.format("No key for value '%s'. Parsing position: %s%n\t%s", value, parser.getLocation(), json));
                    }
                    switch (key) {
                        case ID:
                            if (value != null) {
                                builder.setId(Long.parseLong(value));
                            }
                            break;
                        case NAME:
                            builder.setName(value);
                            break;
                        case STATUS:
                            if (value != null) {
                                builder.setStatus(BatchStatus.valueOf(value));
                            }
                            break;
                        case EXIT_STATUS:
                            builder.setExitStatus(value);
                            break;
                        case CREATE_TIME:
                            if (value != null) {
                                builder.setCreateTime(Long.parseLong(value));
                            }
                            break;
                        case END_TIME:
                            if (value != null) {
                                builder.setEndTime(Long.parseLong(value));
                            }
                            break;
                        case LAST_UPDATE_TIME:
                            if (value != null) {
                                builder.setLastUpdatedTime(Long.parseLong(value));
                            }
                            break;
                        case START_TIME:
                            if (value != null) {
                                builder.setStartTime(Long.parseLong(value));
                            }
                            break;
                        case PROPERTIES:
                            String k = null;
                            while (parser.hasNext()) {
                                final Event e = parser.next();
                                switch (e) {
                                    case KEY_NAME:
                                        k = parser.getString();
                                        break;
                                    case VALUE_FALSE:
                                    case VALUE_NULL:
                                    case VALUE_NUMBER:
                                    case VALUE_STRING:
                                    case VALUE_TRUE:
                                        if (k != null) {
                                            builder.addParameter(k, parser.getString());
                                        }
                                        break;
                                }
                            }
                            break;
                    }
                    break;
            }
        }
        parser.close();
        return builder.build();
    }
View Full Code Here

    private URL url;

    @Test
    public void testJsonServlet() throws Exception {
        final String result = HttpRequest.get(url + "json", 10, TimeUnit.SECONDS);
        final JsonParser parser = Json.createParser(new StringReader(result));
        String key = null;
        String value = null;
        while (parser.hasNext()) {
            final Event event = parser.next();
            switch (event) {
                case KEY_NAME:
                    key = parser.getString();
                    break;
                case VALUE_STRING:
                    value = parser.getString();
                    break;
            }
        }
        parser.close();
        Assert.assertEquals("Key should be \"name\"", "name", key);
        Assert.assertEquals("Value should be \"value\"", "value", value);
    }
View Full Code Here

  }

  public String parsePurchaseOrderAndReturnEmail() throws FileNotFoundException {
    String email = null;

    JsonParser parser = Json.createParser(new FileReader("src/main/resources/order.json"));
    while (parser.hasNext()) {
      JsonParser.Event event = parser.next();
      while (parser.hasNext() && !(event.equals(JsonParser.Event.KEY_NAME) && parser.getString().matches("email"))) {
        event = parser.next();
      }

      if (event.equals(JsonParser.Event.KEY_NAME) && parser.getString().matches("email")) {
        parser.next();
        email = parser.getString();
      }
    }
    return email;
  }
View Full Code Here

    }

    @Override
    public Movie readFrom(Class<Movie> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
        Movie movie = new Movie();
        JsonParser parser = Json.createParser(in);
        while (parser.hasNext()) {
            switch (parser.next()) {
                case KEY_NAME:
                    String key = parser.getString();
                    parser.next();
                    switch (key) {
                        case "id":
                            movie.setId(parser.getInt());
                            break;
                        case "name":
                            movie.setName(parser.getString());
                            break;
                        case "actors":
                            movie.setActors(parser.getString());
                            break;
                        default:
                            break;
                    }
                    break;
View Full Code Here

    public boolean willDecode(String string) {
        boolean decodes = false;
        /* Convert the message into a map */
        messageMap = new HashMap<>();
        logger.fine("String to decode : " + string);
        JsonParser parser = Json.createParser(new StringReader(string));
        while (parser.hasNext()) {
            if (parser.next() == JsonParser.Event.KEY_NAME) {
                String key = parser.getString();
                parser.next();
                String value = parser.getString();
                messageMap.put(key, value);
            }
        }
        /* Check the kind of message and if all fields are included */
        Set keys = messageMap.keySet();
View Full Code Here

            out.println("<body>");
            out.println("<h1>Streaming Reading of JSON from Stream</h1>");

            ServletContext servletContext = request.getServletContext();
            out.println("Reading an empty object<br>");
            JsonParser jsonParser = Json.createParser(servletContext.getResourceAsStream("/1.json"));
            parseEvents(jsonParser, out);

            out.println("<br><br>Reading an object with two elements<br>");
            jsonParser = Json.createParser(servletContext.getResourceAsStream("/2.json"));
            parseEvents(jsonParser, out);
View Full Code Here

            out.println("<body>");
            out.println("<h1>Streaming Reading of JSON from Reader</h1>");

            String json = "{}";
            out.println("Reading an empty object: " + json + "<br>");
            JsonParser jsonParser = Json.createParser(new StringReader(json));
            parseEvents(jsonParser, out);

            json = "{"
                    + "  \"apple\":\"red\","
                    + "  \"banana\":\"yellow\""
View Full Code Here

            Type type1,
            Annotation[] antns,
            MediaType mt, MultivaluedMap<String, String> mm,
            InputStream in) throws IOException, WebApplicationException {
        MyObject mo = new MyObject();
        JsonParser parser = Json.createParser(in);
        while (parser.hasNext()) {
            switch (parser.next()) {
                case KEY_NAME:
                    String key = parser.getString();
                    parser.next();
                    switch (key) {
                        case "name":
                            mo.setName(parser.getString());
                            break;
                        case "age":
                            mo.setAge(parser.getInt());
                            break;
                        default:
                            break;
                    }
                    break;
View Full Code Here

    }

    @Override
    public Movie readFrom(Class<Movie> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
        Movie movie = new Movie();
        JsonParser parser = Json.createParser(in);
        while (parser.hasNext()) {
            switch (parser.next()) {
                case KEY_NAME:
                    String key = parser.getString();
                    parser.next();
                    switch (key) {
                        case "id":
                            movie.setId(parser.getInt());
                            break;
                        case "name":
                            movie.setName(parser.getString());
                            break;
                        case "actors":
                            movie.setActors(parser.getString());
                            break;
                        default:
                            break;
                    }
                    break;
View Full Code Here

TOP

Related Classes of javax.json.stream.JsonParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.