Java Code Examples for com.fasterxml.jackson.core.io.JsonStringEncoder

The following examples show how to use com.fasterxml.jackson.core.io.JsonStringEncoder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mojito   Source File: JsonRawStringAspect.java    License: Apache License 2.0 6 votes vote down vote up
@Around("methods()")
public Object convertReturnedStringToRawJSON(ProceedingJoinPoint pjp) throws Throwable {

    checkMethodHasJsonRawValueAnnotation(pjp);

    String res = (String) pjp.proceed();

    if (!jsonValidator.isValidJsonString(res)) {
        JsonStringEncoder jsonStringEncoder = new JsonStringEncoder();
        StringBuilder sb = new StringBuilder();
        sb.append("\"").append(jsonStringEncoder.quoteAsString(res)).append("\"");
        res = sb.toString();
    }

    return res;
}
 
Example 2
/**
 * Escapes a text to be used for a JSON string value.
 * 
 * @param text the text to escape for JSON
 * @return the escaped text if not null
 */
public static String escapeJSONString(String text)
{
	if (text == null)
	{
		return null;
	}
	
	// using Jackson's string quote method
	char[] escapedChars = JsonStringEncoder.getInstance().quoteAsString(text);
	if (text.contentEquals(CharBuffer.wrap(escapedChars)))
	{
		// nothing changed
		return text;
	}
	
	return String.valueOf(escapedChars);
}
 
Example 3
private void writeValue(Object value)throws IOException {
	if (value != null) {
		if (
				value instanceof Number
						|| value instanceof Boolean
				)
		{
			writer.write(value.toString());
		} else if (value instanceof Date) {
			writer.write("\"");
			writer.write(isoDateFormat.format((Date)value));
			writer.write("\"");
		} else {
			writer.write("\"");
			writer.write(JsonStringEncoder.getInstance().quoteAsString(value.toString()));
			writer.write("\"");
		}
	} else {
		writer.write("null");  // FIXMEJSONMETA: how to treat null values?
	}
}
 
Example 4
Source Project: Elasticsearch   Source File: JsonEscapingMustacheFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encode(String value, Writer writer) {
    try {
        writer.write(JsonStringEncoder.getInstance().quoteAsString(value));
    } catch (IOException e) {
        throw new MustacheException("Failed to encode value: " + value);
    }
}
 
Example 5
public static void writeParts(JasperPrint jasperPrint, Writer writer) throws IOException
{
	PrintParts parts = jasperPrint.getParts();
	writer.write("{");

	writer.write("\"id\": \"parts_" + (parts.hashCode() & 0x7FFFFFFF) + "\",");
	writer.write("\"type\": \"reportparts\",");
	writer.write("\"parts\": [");

	if (!parts.startsAtZero())
	{
		writer.write("{\"idx\": 0, \"name\": \"");
		writer.write(JsonStringEncoder.getInstance().quoteAsString(jasperPrint.getName()));
		writer.write("\"}");
		if (parts.partCount() > 1)
		{
			writer.write(",");
		}
	}

	Iterator<Map.Entry<Integer, PrintPart>> it = parts.partsIterator();

	while (it.hasNext())
	{
		Map.Entry<Integer, PrintPart> partsEntry = it.next();
		int idx = partsEntry.getKey();
		PrintPart part = partsEntry.getValue();
		
		writer.write("{\"idx\": " + idx + ", \"name\": \"");
		writer.write(JsonStringEncoder.getInstance().quoteAsString(part.getName()));
		writer.write("\"}");
		if (it.hasNext())
		{
			writer.write(",");
		}
	}

	writer.write("]");
	writer.write("}");
}
 
Example 6
Source Project: vespa   Source File: JsonWriterTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public final void raw_fields_are_emitted_as_basic_base64() throws IOException {
    // "string long enough to emit more than 76 base64 characters and which should certainly not be newline-delimited!"
    String payload = new String(
            new JsonStringEncoder().quoteAsString(
                    "c3RyaW5nIGxvbmcgZW5vdWdoIHRvIGVtaXQgbW9yZSB0aGFuIDc2IGJhc2U2NCBjaGFyYWN0ZXJzIGFuZC" +
                          "B3aGljaCBzaG91bGQgY2VydGFpbmx5IG5vdCBiZSBuZXdsaW5lLWRlbGltaXRlZCE="));

    String docId = "id:unittest:testraw::whee";

    String fields = "{ \"actualraw\": \"" + payload + "\"" + " }";
    roundTripEquality(docId, fields);
}
 
Example 7
Source Project: vespa   Source File: JsonReaderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRaw() throws IOException {
    String base64 = new String(new JsonStringEncoder().quoteAsString(
            Base64.getEncoder().encodeToString(Utf8.toBytes("smoketest"))));
    String s = fieldStringFromBase64RawContent(base64);
    assertEquals("smoketest", s);
}
 
Example 8
Source Project: herd   Source File: AbstractNotificationMessageBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * JSON escapes a specified string. This method is null-safe.
 *
 * @param input the input string
 *
 * @return the XML escaped string
 */
protected String escapeJson(final String input)
{
    if (input == null)
    {
        return null;
    }
    else
    {
        return String.valueOf(JsonStringEncoder.getInstance().quoteAsString(input));
    }
}
 
Example 9
Source Project: RefactoringMiner   Source File: CodeRange.java    License: MIT License 5 votes vote down vote up
private String escapeQuotes(String s) {
	if(s != null) {
		StringBuilder sb = new StringBuilder();
		JsonStringEncoder encoder = BufferRecyclers.getJsonStringEncoder();
		encoder.quoteAsString(s, sb);
		return sb.toString();
	}
	return s;
}
 
Example 10
Source Project: RefactoringMiner   Source File: Refactoring.java    License: MIT License 5 votes vote down vote up
default public String toJSON() {
	StringBuilder sb = new StringBuilder();
	JsonStringEncoder encoder = BufferRecyclers.getJsonStringEncoder();
	sb.append("{").append("\n");
	sb.append("\t").append("\"").append("type").append("\"").append(": ").append("\"").append(getName()).append("\"").append(",").append("\n");
	sb.append("\t").append("\"").append("description").append("\"").append(": ").append("\"");
	encoder.quoteAsString(toString().replace('\t', ' '), sb);
	sb.append("\"").append(",").append("\n");
	sb.append("\t").append("\"").append("leftSideLocations").append("\"").append(": ").append(leftSide()).append(",").append("\n");
	sb.append("\t").append("\"").append("rightSideLocations").append("\"").append(": ").append(rightSide()).append("\n");
	sb.append("}");
	return sb.toString();
}
 
Example 11
Source Project: pnc   Source File: OpenshiftStartedEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return an escaped string of the JSON representation of the object
 *
 * By 'escaped', it means that strings like '"' are escaped to '\"'
 * 
 * @param object object to marshall
 * @return Escaped Json String
 */
private String toEscapedJsonString(Object object) {
    ObjectMapper mapper = new ObjectMapper();
    JsonStringEncoder jsonStringEncoder = JsonStringEncoder.getInstance();
    try {
        return new String(jsonStringEncoder.quoteAsString(mapper.writeValueAsString(object)));
    } catch (JsonProcessingException e) {
        logger.error("Could not parse object: " + object, e);
        throw new RuntimeException(e);
    }
}
 
Example 12
Source Project: XRTB   Source File: TestEncoding.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String [] args) {
	 JsonStringEncoder encoder = JsonStringEncoder.getInstance();
	 String test = "This is \\\\\" a test";
	char[] output =  encoder.quoteAsString(test);
	String end = new String(output);
	System.out.println(test);;
	System.out.println(end);;
}
 
Example 13
Source Project: presto   Source File: CassandraCqlUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String quoteStringLiteralForJson(String string)
{
    return '"' + new String(JsonStringEncoder.getInstance().quoteAsUTF8(string), UTF_8) + '"';
}
 
Example 14
Source Project: spring-analysis-note   Source File: Jackson2SockJsMessageCodec.java    License: MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example 15
Source Project: java-technology-stack   Source File: Jackson2SockJsMessageCodec.java    License: MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example 16
Source Project: centraldogma   Source File: Jackson.java    License: Apache License 2.0 4 votes vote down vote up
public static String escapeText(String text) {
    final JsonStringEncoder enc = JsonStringEncoder.getInstance();
    return new String(enc.quoteAsString(text));
}
 
Example 17
Source Project: scava   Source File: IndexEntry.java    License: Eclipse Public License 2.0 4 votes vote down vote up
@Override
	public String getContents() {
		JsonStringEncoder encoder = JsonStringEncoder.getInstance();
//		BufferRecycler encoder = BufferRecyclers.getJsonStringEncoder();
		return new String(encoder.quoteAsString(this.adapter.body()));
	}
 
Example 18
@Override
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example 19
@Override
public void encode(String s, Writer writer) throws IOException {
    writer.write(JsonStringEncoder.getInstance().quoteAsString(s));
}
 
Example 20
Source Project: ameba   Source File: Jackson2SockJsMessageCodec.java    License: MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected char[] applyJsonQuoting(String content) {
    return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example 21
Source Project: usergrid   Source File: JsonUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String quoteString( String s ) {
    JsonStringEncoder encoder = new JsonStringEncoder();
    return new String( encoder.quoteAsUTF8( s ) );
}
 
Example 22
Source Project: tablesaw   Source File: Utils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Escapes string for Javascript, assuming but without surrounding it with doublequotes (") and
 * saves to output to the given StringBuilder.
 */
private static void escape(String s, StringBuilder sb) {
  JsonStringEncoder.getInstance().quoteAsString(s, sb);
}
 
Example 23
Source Project: tablesaw   Source File: Utils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Escapes string for Javascript, assuming but without surrounding it with doublequotes (") and
 * saves to output to the given StringBuilder.
 */
private static void escape(String s, StringBuilder sb) {
  JsonStringEncoder.getInstance().quoteAsString(s, sb);
}