当前位置: 首页>>代码示例>>Java>>正文


Java JsonParser类代码示例

本文整理汇总了Java中javax.json.stream.JsonParser的典型用法代码示例。如果您正苦于以下问题:Java JsonParser类的具体用法?Java JsonParser怎么用?Java JsonParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


JsonParser类属于javax.json.stream包,在下文中一共展示了JsonParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: parseJsonString

import javax.json.stream.JsonParser; //导入依赖的package包/类
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;
            }
        }
    }
 
开发者ID:readlearncode,项目名称:JSON-Processing-with-Java-EE,代码行数:27,代码来源:StreamingExample1.java

示例2: deserialize

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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;
}
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:18,代码来源:BookDeserializer.java

示例3: buildArray

import javax.json.stream.JsonParser; //导入依赖的package包/类
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();
}
 
开发者ID:i49,项目名称:hibiscus,代码行数:17,代码来源:JsonValidatingReader.java

示例4: retrieveValue

import javax.json.stream.JsonParser; //导入依赖的package包/类
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;
    }
 
开发者ID:readlearncode,项目名称:JSON-Processing-with-Java-EE,代码行数:21,代码来源:StreamingExample2.java

示例5: deserializeInternal

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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();
    }
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:18,代码来源:UserDeserializerDeserializer.java

示例6: testLocation4

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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 ());
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:24,代码来源:UTF8TextJsonParserTest.java

示例7: testZero3

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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 ();
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:20,代码来源:UTF8TextJsonParserTest.java

示例8: deserialize

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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());
    }
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:18,代码来源:AdaptedObjectDeserializer.java

示例9: deserialize

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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));
    }
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:18,代码来源:JsonValueDeserializer.java

示例10: testInputStream

import javax.json.stream.JsonParser; //导入依赖的package包/类
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 ());
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:27,代码来源:JsonParserFactoryTest.java

示例11: testGetDecimal

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:22,代码来源:UTF8TextJsonParserTest.java

示例12: testGetDecimal

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:TextJsonParserTest.java

示例13: testGetInt_2

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:TextJsonParserTest.java

示例14: testGetLong_2

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:TextJsonParserTest.java

示例15: testJsonHex

import javax.json.stream.JsonParser; //导入依赖的package包/类
@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);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:24,代码来源:BinaryTest.java


注:本文中的javax.json.stream.JsonParser类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。