Java 类javax.json.stream.JsonParser 实例源码

项目:JSON-Processing-with-Java-EE    文件:StreamingExample1.java   
private static void parseJsonString() {

        JsonParser parser = Json.createParser(new StringReader(JSON));

        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;
            }
        }
    }
项目:Java-EE-8-Sampler    文件:BookDeserializer.java   
@Override
public Book deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
    Book book = null;
    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        if (event == JsonParser.Event.KEY_NAME) {
            String className = parser.getString();
            parser.next();
            try {
                book = ctx.deserialize(Class.forName(className).asSubclass(Book.class), parser);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    return book;
}
项目:JSON-Processing-with-Java-EE    文件:StreamingExample2.java   
public String retrieveValue(final String key_to_find, final String json) {

        JsonParser parser = Json.createParser(new StringReader(json));

        while (parser.hasNext()) {
            JsonParser.Event event = parser.next();
            switch (event) {
                case KEY_NAME:
                    if (parser.getString().equalsIgnoreCase(key_to_find)) {
                        event = parser.next();
                        if (event == VALUE_STRING || event == VALUE_NUMBER) {
                            return parser.getString();
                        }
                    }
                    break;
            }
        }

        return null;
    }
项目:hibiscus    文件:JsonValidatingReader.java   
private JsonArray buildArray(ArrayType type) {
    ArrayContext context = new ArrayContext(this.transientValueProvider, this.builderFactory);
    pushContext(context);
    TypeSet itemTypes = type.getItemTypes();
    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        if (event == JsonParser.Event.END_ARRAY) {
            popContext();
            return context.getArray();
        } else {
            context.nextItem();
            readValue(event, itemTypes);
        }
    }
    throw internalError();
}
项目:yasson    文件:UserDeserializerDeserializer.java   
@SuppressWarnings("unchecked")
@Override
public void deserializeInternal(JsonbParser parser, Unmarshaller context) {
    parserContext = moveToFirst(parser);
    JsonParser.Event lastEvent = parserContext.getLastEvent();
    final UserDeserializerParser userDeserializerParser = new UserDeserializerParser(parser);
    try {
        context.getJsonbContext().addProcessedType(deserializerBinding.getBindingType());
        deserializerResult = (T) deserializerBinding.getJsonbDeserializer().deserialize(userDeserializerParser, context, getRuntimeType());
    } finally {
        context.getJsonbContext().removeProcessedType(deserializerBinding.getBindingType());
    }
    //Avoid moving parser to the end of the object, if deserializer was for one value only.
    if (lastEvent == JsonParser.Event.START_ARRAY || lastEvent == JsonParser.Event.START_OBJECT) {
        userDeserializerParser.advanceParserToEnd();
    }
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testLocation4 ()
{
    String json = "{\"abc\\t\" : []}";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    JsonLocation location = null;
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case START_ARRAY:
                location = p.getLocation ();
                break;
            default:
                break;
        }
    }
    p.close ();
    Assert.assertEquals (1, location.getLineNumber ());
    Assert.assertEquals (12, location.getColumnNumber ());
    Assert.assertEquals (11, location.getStreamOffset ());
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testZero3 ()
{
    String json = "[ -0E0 ]";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case VALUE_NUMBER:
                Assert.assertEquals ("-0E0", p.getString ());
                break;
            default:
                break;
        }
    }
    p.close ();
}
项目:yasson    文件:AdaptedObjectDeserializer.java   
@Override
@SuppressWarnings("unchecked")
public T deserialize(JsonParser parser, DeserializationContext context, Type rtType) {
    Unmarshaller unmarshaller = (Unmarshaller) context;
    unmarshaller.setCurrent(this);
    try {
        unmarshaller.getJsonbContext().addProcessedType(adapterInfo.getBindingType());
        final A result =  adaptedTypeDeserializer.deserialize(parser, context, rtType);
        final T adapted = ((JsonbAdapter<T, A>) adapterInfo.getAdapter()).adaptFromJson(result);
        unmarshaller.setCurrent(wrapperItem);
        return adapted;
    } catch (Exception e) {
        throw new JsonbException(Messages.getMessage(MessageKeys.ADAPTER_EXCEPTION, adapterInfo.getBindingType(), adapterInfo.getToType(), adapterInfo.getAdapter().getClass()), e);
    } finally {
        unmarshaller.getJsonbContext().removeProcessedType(adapterInfo.getBindingType());
    }
}
项目:yasson    文件:JsonValueDeserializer.java   
@Override
public JsonValue deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
    final JsonParser.Event next = ((JsonbRiParser)parser).getLastEvent();
    switch (next) {
        case VALUE_TRUE:
            return JsonValue.TRUE;
        case VALUE_FALSE:
            return JsonValue.FALSE;
        case VALUE_NULL:
            return JsonValue.NULL;
        case VALUE_STRING:
        case VALUE_NUMBER:
            return parser.getValue();
        default:
            throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, "Unknown JSON value: "+next));
    }
}
项目:cookjson    文件:JsonParserFactoryTest.java   
void testInputStream (String fileName, HashMap<String, Object> config) throws IOException
{
    File file = new File (fileName.replace ('/', File.separatorChar));

    JsonProvider provider1 = new CookJsonProvider ();

    JsonParserFactory f1 = provider1.createParserFactory (config);

    StringWriter out1 = new StringWriter ();
    JsonParser p1 = f1.createParser (new FileInputStream (file));
    JsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider2 = new org.glassfish.json.JsonProviderImpl ();
    JsonParserFactory f2 = provider2.createParserFactory (config);
    JsonParser p2 = f2.createParser (new FileInputStream (file));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out1.toString (), out2.toString ());
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testGetDecimal () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 0);
    BigDecimal[] decimals = new BigDecimal[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            decimals[count++] = p.getBigDecimal ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new BigDecimal[]
    { new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"),
            new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L),
            new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1) },
            decimals);
}
项目:cookjson    文件:TextJsonParserTest.java   
@Test
public void testGetDecimal () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 0);
    BigDecimal[] decimals = new BigDecimal[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            decimals[count++] = p.getBigDecimal ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new BigDecimal[]{ new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1) }, decimals);
}
项目:cookjson    文件:TextJsonParserTest.java   
@Test
public void testGetInt_2 () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 1);
    int[] ints = new int[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            ints[count++] = p.getInt ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new int[]{ 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints);
}
项目:cookjson    文件:TextJsonParserTest.java   
@Test
public void testGetLong_2 () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 1);
    long[] longs = new long[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            longs[count++] = p.getLong ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new long[]{ 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }, longs);
}
项目:cookjson    文件:BinaryTest.java   
@Test
public void testJsonHex () throws IOException
{
    File file1 = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar));
    File file2 = new File ("../tests/data/binary2.json".replace ('/', File.separatorChar));

    CookJsonProvider provider = new CookJsonProvider ();
    HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
    bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
    bsonConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);

    HashMap<String, Object> jsonConfig = new HashMap<String, Object> ();
    jsonConfig.put (CookJsonProvider.BINARY_FORMAT, CookJsonProvider.BINARY_FORMAT_HEX);

    File jsonFile = testFolder.newFile ();
    JsonParser p = provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
    JsonGenerator g = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile));
    Utils.convert (p, g);
    p.close ();
    g.close ();

    FileAssert.assertBinaryEquals (file2, jsonFile);
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testZero1 ()
{
    String json = "[ -0 ]";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case VALUE_NUMBER:
                Assert.assertEquals ("-0", p.getString ());
                break;
            default:
                break;
        }
    }
    p.close ();
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testLocation5 ()
{
    String json = "[ true, false, null ]";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    JsonLocation location = null;
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case VALUE_TRUE:
                location = p.getLocation ();
                break;
            default:
                break;
        }
    }
    p.close ();
    Assert.assertEquals (1, location.getLineNumber ());
    Assert.assertEquals (3, location.getColumnNumber ());
    Assert.assertEquals (2, location.getStreamOffset ());
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testLocation7 ()
{
    String json = "[ true, false, null ]";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    JsonLocation location = null;
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case VALUE_NULL:
                location = p.getLocation ();
                break;
            default:
                break;
        }
    }
    p.close ();
    Assert.assertEquals (1, location.getLineNumber ());
    Assert.assertEquals (16, location.getColumnNumber ());
    Assert.assertEquals (15, location.getStreamOffset ());
}
项目:cookjson    文件:TextJsonParserTest.java   
void testFile (String f, int buffType) throws IOException
{
    File file = new File (f.replace ('/', File.separatorChar));

    StringWriter out1 = new StringWriter ();
    TextJsonParser p1 = getJsonParser (file, buffType);
    TextJsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
    JsonParser p2 = provider.createParser (new FileInputStream (file));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out2.toString (), out1.toString ());
}
项目:cookjson    文件:UTF8TextJsonParserErrorTest.java   
@Test (expected = IllegalStateException.class)
@SuppressWarnings ("resource")
public void testStateError ()
{
    String json = "{\"abc\":1234}";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    for (;;)
    {
        switch (p.next ())
        {
            case START_OBJECT:
                p.getString ();
                break;
            default:
                break;
        }
    }
}
项目:cookjson    文件:UTF8CommentTest.java   
void testFile (String f1, String f2, boolean smallBuf) throws IOException
{
    File file1 = new File (f1.replace ('/', File.separatorChar));
    File file2 = new File (f2.replace ('/', File.separatorChar));

    StringWriter out1 = new StringWriter ();
    JsonParser p1 = getJsonParser (file1, smallBuf);
    JsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
    JsonParser p2 = provider.createParser (new FileInputStream (file2));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out1.toString (), out2.toString ());
}
项目:cookjson    文件:BinaryTest.java   
@Test
public void testBson () throws IOException
{
    File file1 = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar));

    CookJsonProvider provider = new CookJsonProvider ();
    HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
    bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
    bsonConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);

    // first convert from Json to Bson using stream API
    File bsonFile = testFolder.newFile ();
    JsonParser p = provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
    JsonGenerator g = provider.createGeneratorFactory (bsonConfig).createGenerator (new FileOutputStream (bsonFile));
    Utils.convert (p, g);
    p.close ();
    g.close ();

    BsonFixLength.fix (bsonFile);
    FileAssert.assertBinaryEquals (file1, bsonFile);
}
项目:cookjson    文件:JsonParserFactoryTest.java   
void testReader (String fileName, HashMap<String, Object> config) throws IOException
{
    File file = new File (fileName.replace ('/', File.separatorChar));

    JsonProvider provider1 = new CookJsonProvider ();

    JsonParserFactory f1 = provider1.createParserFactory (config);

    StringWriter out1 = new StringWriter ();
    JsonParser p1 = f1.createParser (new InputStreamReader (new FileInputStream (file), BOM.utf8));
    JsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider2 = new org.glassfish.json.JsonProviderImpl ();
    JsonParserFactory f2 = provider2.createParserFactory (config);
    JsonParser p2 = f2.createParser (new InputStreamReader (new FileInputStream (file), BOM.utf8));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out1.toString (), out2.toString ());
}
项目:cookjson    文件:BOMTest.java   
private void testFile (Charset charset) throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));

    CookJsonProvider provider = new CookJsonProvider ();
    ByteArrayOutputStream bos = new ByteArrayOutputStream ();
    JsonParser p = TextJsonConfigHandler.getJsonParser (new FileInputStream (file));
    JsonGenerator g = provider.createGeneratorFactory (new HashMap<String, Object> ()).createGenerator (bos, charset);
    Utils.convert (p, g);
    p.close ();
    g.close ();

    StringWriter sw = new StringWriter ();
    p = provider.createParser (new ByteArrayInputStream (bos.toByteArray ()));
    g = provider.createGenerator (sw);
    Utils.convert (p, g);
    p.close ();
    g.close ();

    Assert.assertEquals (Utils.getString (file), sw.toString ());
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testNumber6 ()
{
    String json = "{\"abc\" : -0.1234567890 }";

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    while (p.hasNext ())
    {
        switch (p.next ())
        {
            case VALUE_NUMBER:
                Assert.assertEquals ("-0.1234567890", p.getString ());
                break;
            default:
                break;
        }
    }
    p.close ();
}
项目:cookjson    文件:CommentTest.java   
void testFile (String f1, String f2, boolean smallBuf) throws IOException
{
    File file1 = new File (f1.replace ('/', File.separatorChar));
    File file2 = new File (f2.replace ('/', File.separatorChar));

    StringWriter out1 = new StringWriter ();
    JsonParser p1 = getJsonParser (file1, smallBuf);
    JsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
    JsonParser p2 = provider.createParser (new FileInputStream (file2));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out1.toString (), out2.toString ());
}
项目:cookjson    文件:BsonParserTest.java   
void testFile (String f1, String f2) throws IOException
{
    File file1 = new File (f1.replace ('/', File.separatorChar));
    File file2 = new File (f2.replace ('/', File.separatorChar));

    StringWriter out1 = new StringWriter ();
    BsonParser p1 = new BsonParser (new FileInputStream (file1));
    p1.setRootAsArray (true);
    Assert.assertEquals (true, p1.isRootAsArray ());
    TextJsonGenerator g1 = new TextJsonGenerator (out1);
    Utils.convert (p1, g1);
    p1.close ();
    g1.close ();

    StringWriter out2 = new StringWriter ();
    JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
    JsonParser p2 = provider.createParser (new FileInputStream (file2));
    JsonGenerator g2 = new TextJsonGenerator (out2);
    Utils.convert (p2, g2);
    p2.close ();
    g2.close ();

    Assert.assertEquals (out1.toString (), out2.toString ());
}
项目:cookjson    文件:BinaryTest.java   
@Test
public void testJson () throws IOException
{
    File file1 = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar));
    File file2 = new File ("../tests/data/binary.json".replace ('/', File.separatorChar));

    CookJsonProvider provider = new CookJsonProvider ();
    HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
    bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
    bsonConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);

    HashMap<String, Object> jsonConfig = new HashMap<String, Object> ();

    File jsonFile = testFolder.newFile ();
    JsonParser p = provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
    JsonGenerator g = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile));
    Utils.convert (p, g);
    p.close ();
    g.close ();

    FileAssert.assertBinaryEquals (file2, jsonFile);
}
项目:cookjson    文件:BsonParserTest.java   
@Test
public void testGetDecimal () throws IOException
{
    File file = new File ("../tests/data/types.bson".replace ('/', File.separatorChar));
    JsonParser p = new BsonParser (new FileInputStream (file));
    BigDecimal[] decimals = new BigDecimal[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            decimals[count++] = p.getBigDecimal ();
        }
    }
    p.close ();
    BigDecimal d = new BigDecimal (new BigInteger ("1234567890123412345678901234").doubleValue ());
    Assert.assertArrayEquals (new BigDecimal[]{ new BigDecimal (1234), new BigDecimal (12345678901234L), d, new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), d, new BigDecimal (12345.5), new BigDecimal (1) }, decimals);
}
项目:cookjson    文件:TextJsonParserTest.java   
@Test
public void testGetInt () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 0);
    int[] ints = new int[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            ints[count++] = p.getInt ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new int[]{ 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints);
}
项目:cookjson    文件:UTF8TextJsonParserTest.java   
@Test
public void testGetLong () throws IOException
{
    File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
    JsonParser p = getJsonParser (file, 0);
    long[] longs = new long[9];
    int count = 0;
    while (p.hasNext ())
    {
        if (p.next () == Event.VALUE_NUMBER)
        {
            longs[count++] = p.getLong ();
        }
    }
    p.close ();
    Assert.assertArrayEquals (new long[]
    { 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 },
            longs);
}
项目:Java-EE-8-Sampler    文件:BookDeserializer.java   
@Override
public String deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        if (event == JsonParser.Event.KEY_NAME) {
            String keyName = parser.getString();
            if (keyName.equals("id")) {
                return ctx.deserialize(String.class, parser);
            }
        }
        parser.next();
    }
    return "";
}
项目:ee8-sandbox    文件:JsonpTest.java   
@Test
public void testJsonParser() {
    String key = "";
    String value = "";
    JsonParser parser = Json.createParser(JsonpTest.class.getResourceAsStream("/person.json"));
    while (parser.hasNext()) {
        JsonParser.Event e = parser.next();
        System.out.print(e.name());

        switch (e) {
            case START_ARRAY:
                if (key.equals("phoneNumbers")) {
                    parser.skipArray();
                }
                break;
            case KEY_NAME:
                key = parser.getString();
                System.out.print(" - " + parser.getString());
                break;
            case VALUE_STRING:
                System.out.print(" - " + parser.getString());
                if ("name".equals(key)) {
                    value = parser.getString();
                }
        }

        System.out.println();
    }

    assertEquals("Duke", value);
}
项目:hibiscus    文件:BasicJsonValidator.java   
@Override
public ValidationResult validate(Reader reader) {
    if (reader == null) {
        throw new IllegalArgumentException("reader is null.");
    }
    try (JsonParser parser = this.parserFactory.createParser(reader)) {
        return parse(parser);
    }
}
项目:cookjson    文件:UTF8TextJsonParserErrorTest.java   
@Test
@SuppressWarnings ("resource")
public void testNumber7 ()
{
    String json = "[ -\u0000 ]";

    thrown.expect (JsonParsingException.class);
    thrown.expectMessage ("Parsing error at line 1, column 4, offset 3: unexpected character '\\u0000'");

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)));
    for (;;)
    {
        p.next ();
    }
}
项目:hibiscus    文件:BasicJsonValidator.java   
@Override
public ValidationResult validate(InputStream stream, Charset charset) {
    if (stream == null) {
        throw new IllegalArgumentException("stream is null.");
    }
    if (charset == null) {
        throw new IllegalArgumentException("charset is null.");
    }
    try (JsonParser parser = this.parserFactory.createParser(stream, charset)) {
        return parse(parser);
    }
}
项目:hibiscus    文件:JsonValidatingReader.java   
/**
 * Reads JSON array, object, or other values.
 * @param event the event which {@link JsonParser} emits.
 * @param candidates the type candidates of the value to be read. 
 */
private void readValue(JsonParser.Event event, TypeSet candidates) {
    if (event == JsonParser.Event.START_ARRAY) {
        readArray(candidates);
    } else if (event == JsonParser.Event.START_OBJECT) {
        readObject(candidates);
    } else {
        readAtomicValue(event, candidates);
    }
}
项目:cookjson    文件:UTF8TextJsonParserErrorTest.java   
@Test
@SuppressWarnings ("resource")
public void testNumber7_2 ()
{
    String json = "[ -\u0000 ]";

    thrown.expect (JsonParsingException.class);
    thrown.expectMessage ("Parsing error at line 1, column 4, offset 3: unexpected character '\\u0000'");

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)), 2);
    for (;;)
    {
        p.next ();
    }
}
项目:cookjson    文件:UTF8TextJsonParserErrorTest.java   
@Test
@SuppressWarnings ("resource")
public void testNumber9_2 ()
{
    String json = "[ 0.5e5\u0000 ]";

    thrown.expect (JsonParsingException.class);
    thrown.expectMessage ("Parsing error at line 1, column 8, offset 7: unexpected character '\\u0000'");

    JsonParser p = new UTF8TextJsonParser (new ByteArrayInputStream (json.getBytes (BOM.utf8)), 2);
    for (;;)
    {
        p.next ();
    }
}
项目:Java-EE-7-Development-with-NetBeans-8    文件:JsonPStreamingApiBean.java   
public String parseJson() {
    StringReader stringReader = new StringReader(jsonStr);

    JsonParser jsonParser = Json.createParser(stringReader);

    Map<String, Object> jsonMap = new HashMap<>();
    String jsonKeyNm = null;
    Object jsonVal = null;

    while (jsonParser.hasNext()) {
        JsonParser.Event event = jsonParser.next();

        if (event.equals(Event.KEY_NAME)) {
            jsonKeyNm = jsonParser.getString();
        } else if (event.equals(Event.VALUE_STRING)) {
            jsonVal = jsonParser.getString();
        } else if (event.equals(Event.VALUE_NUMBER)) {
            jsonVal = jsonParser.getInt();
        }

        jsonMap.put(jsonKeyNm, jsonVal);
    }

    person.setFirstName((String) jsonMap.get("firstName"));
    person.setMiddleName((String) jsonMap.get("middleName"));
    person.setLastName((String) jsonMap.get("lastName"));
    person.setGender((String) jsonMap.get("gender"));
    person.setAge((Integer) jsonMap.get("age"));

    return "display_populated_obj";
}