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


Java JsonGenerator类代码示例

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


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

示例1: onCargoInspected

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
public void onCargoInspected(@Observes @CargoInspected Cargo cargo) {
    Writer writer = new StringWriter();

    try (JsonGenerator generator = Json.createGenerator(writer)) {
        generator
                .writeStartObject()
                .write("trackingId", cargo.getTrackingId().getIdString())
                .write("origin", cargo.getOrigin().getName())
                .write("destination", cargo.getRouteSpecification().getDestination().getName())
                .write("lastKnownLocation", cargo.getDelivery().getLastKnownLocation().getName())
                .write("transportStatus", cargo.getDelivery().getTransportStatus().toString())
                .writeEnd();
    }

    String jsonValue = writer.toString();

    for (Session session : sessions) {
        try {
            session.getBasicRemote().sendText(jsonValue);
        } catch (IOException ex) {
            logger.log(Level.WARNING, "Unable to publish WebSocket message", ex);
        }
    }

}
 
开发者ID:RETIT,项目名称:continuous-performance-evaluation,代码行数:26,代码来源:RealtimeCargoTrackingService.java

示例2: build

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
/**
    * Returns a name and a value after the OK
    * @return just OK
    */
   public static String build(String name, String value) {

if (name == null) {
    throw new NullPointerException("name is null");
}
if (value == null) {
    throw new NullPointerException("value is null");
}

JsonGeneratorFactory jf = JsonUtil.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject()
.write("status", "OK")	  
.write(name, value)
.writeEnd();
gen.close();

return sw.toString();
   }
 
开发者ID:kawansoft,项目名称:aceql-http,代码行数:27,代码来源:JsonOkReturn.java

示例3: statementNotAllowedBuild

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
/**
    * Builds a security error message in JSON format for Statements
    * @param sqlOrder
    * @param errorMessage
    * @param doPrettyPrinting
    * @return
    */
   public static String statementNotAllowedBuild(String sqlOrder, String errorMessage, boolean doPrettyPrinting) {

try {
    JsonGeneratorFactory jf = JsonUtil
    	.getJsonGeneratorFactory(doPrettyPrinting);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    JsonGenerator gen = jf.createGenerator(out);
    gen.writeStartObject();
    gen.write(Tag.PRODUCT_SECURITY, errorMessage);
    gen.write("SQL order", sqlOrder);
    gen.writeEnd();
    gen.close();
    return out.toString("UTF-8");
} catch (Exception e) {
    String returnString = Tag.PRODUCT_SECURITY + " " + errorMessage
	    + " " + sqlOrder;
    return returnString;
}

   }
 
开发者ID:kawansoft,项目名称:aceql-http,代码行数:30,代码来源:JsonSecurityMessage.java

示例4: jsonStudentList

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
private static String jsonStudentList(List<Student> students) {

        StringWriter swriter = new StringWriter();
        try (JsonGenerator gen = Json.createGenerator(swriter)) {
            gen.writeStartObject();
            gen.write("type", "list");
            gen.writeStartArray("body");
            for (Student student : students) {
                gen.writeStartObject();
                gen.write("name", student.getName());
                gen.write("status", student.getStatus().toString());
                gen.writeEnd();
            }
            gen.writeEnd();
            gen.writeEnd();
        }

        return swriter.toString();
    }
 
开发者ID:wwu-pi,项目名称:tap17-muggl-javaee,代码行数:20,代码来源:StatusEndpoint.java

示例5: testFile

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

示例6: formatBody

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
private String formatBody(String contentType, String body) {
	if (body != null && contentType != null) {
		if (contentType.startsWith("application/json")) {
			try {
				JsonStructure json = Json.createReader(new StringReader(body)).read();
				StringWriter w = new StringWriter();
				HashMap<String, Object> p = new HashMap<>();
				p.put(JsonGenerator.PRETTY_PRINTING, true);
				Json.createWriterFactory(p).createWriter(w).write(json);
				return w.toString();
			} catch (JsonException ex) {
				return body;
			}
		} else {
			return body;
		}
	} else {
		return null;
	}
}
 
开发者ID:RUB-NDS,项目名称:PrOfESSOS,代码行数:21,代码来源:TestStepLogger.java

示例7: testInputStream

import javax.json.stream.JsonGenerator; //导入依赖的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

示例8: testJsonValueJsonHex

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Test
public void testJsonValueJsonHex () 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 ();
	CookJsonParser p = (CookJsonParser) provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
	p.next ();
	JsonValue value = p.getValue ();
	JsonGenerator g = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile));
	g.write (value);
	p.close ();
	g.close ();

	FileAssert.assertBinaryEquals (file2, jsonFile);
}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:26,代码来源:BinaryTest.java

示例9: write

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
	public JsonGenerator write (JsonValue value)
	{
//		assert Debug.debug ("WRITE: JsonValue");
		if (m_state != GeneratorState.IN_ARRAY)
		{
			if (m_state == GeneratorState.INITIAL)
			{
				if (!(value instanceof JsonArray) &&
					!(value instanceof JsonObject))
					throw new JsonGenerationException (ErrorMessage.invalidContext);
			}
			else
				throw new JsonGenerationException (ErrorMessage.notInArrayContext);
		}
		return writeValue (value);
	}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:BsonGenerator.java

示例10: toJson

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
public void toJson(JsonGenerator generator) {
    generator.write("workflowId", workflowId);
    generator.writeStartArray("ruleIds");
    for (RuleId ruleId : ruleIds) {
        generator.writeStartObject();
        generator.write("attributeSet", ruleId.getAttributeSet().toString());
        generator.writeStartObject("securityIndexId");
        generator.write("contingencyId", ruleId.getSecurityIndexId().getContingencyId());
        generator.writeStartObject("securityIndexType");
        generator.write("name", ruleId.getSecurityIndexId().getSecurityIndexType().name());
        generator.write("label", ruleId.getSecurityIndexId().getSecurityIndexType().getLabel());
        generator.write("dynamic", ruleId.getSecurityIndexId().getSecurityIndexType().isDynamic());
        generator.writeEnd();
        generator.writeEnd();
        generator.writeEnd();
    }
    generator.writeEnd();
}
 
开发者ID:itesla,项目名称:ipst,代码行数:20,代码来源:SecurityRulesChangeMessage.java

示例11: write

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
	public JsonGenerator write (BigInteger value)
	{
//		assert Debug.debug ("WRITE: VALUE_NUMBER: (BigInteger)" + value);
		if (m_state != GeneratorState.IN_ARRAY)
			throw new JsonGenerationException (ErrorMessage.notInArrayContext);
		try
		{
			writeComma ();
			w (value.toString ());
		}
		catch (IOException ex)
		{
			throw new JsonGenerationException (ex.getMessage (), ex);
		}
		return this;
	}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:TextJsonGenerator.java

示例12: toJson

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
public void toJson(JsonGenerator generator) {
    generator.write("workflowId", workflowId);
    generator.writeStartArray("samplesSynthesis");
    for (SampleSynthesis sample : samples) {
        generator.writeStartObject()
                    .write("id", sample.getId())
                    .writeStartObject("lastTaskEvent")
                        .write("type", sample.getLastTaskEvent().getTaskType().name())
                        .write("status", sample.getLastTaskEvent().getTaskStatus().name());
        if (sample.getLastTaskEvent().getFailureReason() != null) {
            generator.write("failureReason", sample.getLastTaskEvent().getFailureReason());
        }
        generator.writeEnd()
                .writeEnd();
    }
    generator.writeEnd();
}
 
开发者ID:itesla,项目名称:ipst,代码行数:19,代码来源:SamplesSynthesisMessage.java

示例13: whiteboard

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@RequestMapping(value = "whiteboard", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + "; " + CHARSET)
public String whiteboard() {
    try (StringWriter stringWriter = new StringWriter(); JsonGenerator jsonGenerator = Json.createGenerator(stringWriter)) {
        jsonGenerator.writeStartArray();
        service.listItems().stream()
                .map(item -> Json.createObjectBuilder()
                        .add("id", item.getId())
                        .add("title", item.getTitle())
                        .add("creationDate", item.getCreationDate())
                        .build())
                .forEach(jsonGenerator::write);
        jsonGenerator.writeEnd();
        jsonGenerator.close();
        return stringWriter.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "[]";
}
 
开发者ID:gluonhq,项目名称:gluon-samples,代码行数:21,代码来源:FrontHandler.java

示例14: writeNull

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
	public JsonGenerator writeNull ()
	{
//		assert Debug.debug ("WRITE: VALUE_NULL");
		if (m_state != GeneratorState.IN_ARRAY)
			throw new JsonGenerationException (ErrorMessage.notInArrayContext);
		try
		{
			writeComma ();
			w ("null");
		}
		catch (IOException ex)
		{
			throw new JsonGenerationException (ex.getMessage (), ex);
		}
		return this;
	}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:18,代码来源:TextJsonGenerator.java

示例15: writeEnd

import javax.json.stream.JsonGenerator; //导入依赖的package包/类
@Override
	public JsonGenerator writeEnd ()
	{
		if (m_state == GeneratorState.END)
			throw new JsonGenerationException (ErrorMessage.invalidContext);
		boolean isArray = popState ();
//		assert Debug.debug ("WRITE: " + (isArray ? "END_ARRAY" : "END_OBJECT"));
		m_first = false;
		char ch = isArray ? ']' : '}';
		try
		{
			w (ch);
		}
		catch (IOException ex)
		{
			throw new JsonGenerationException (ex.getMessage (), ex);
		}
		return this;
	}
 
开发者ID:coconut2015,项目名称:cookjson,代码行数:20,代码来源:TextJsonGenerator.java


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