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

The following examples show how to use com.fasterxml.jackson.core.io.JsonStringEncoder. 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 File: JsonRawStringAspect.java    From mojito with 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
Source File: JRStringUtil.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: JsonMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 File: TestEncoding.java    From XRTB with 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 #5
Source File: JsonEscapingMustacheFactory.java    From Elasticsearch with 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 #6
Source File: OpenshiftStartedEnvironment.java    From pnc with 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 #7
Source File: Refactoring.java    From RefactoringMiner with 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 #8
Source File: JsonExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 #9
Source File: CodeRange.java    From RefactoringMiner with 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 File: JsonWriterTestCase.java    From vespa with 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 #11
Source File: JsonReaderTestCase.java    From vespa with 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 #12
Source File: AbstractNotificationMessageBuilder.java    From herd with 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 #13
Source File: CustomMustacheFactory.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(String s, Writer writer) throws IOException {
    writer.write(JsonStringEncoder.getInstance().quoteAsString(s));
}
 
Example #14
Source File: JsonUtils.java    From usergrid with 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 #15
Source File: Jackson2SockJsMessageCodec.java    From ameba with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected char[] applyJsonQuoting(String content) {
    return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example #16
Source File: CassandraCqlUtils.java    From presto with 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 #17
Source File: Jackson2SockJsMessageCodec.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example #18
Source File: IndexEntry.java    From scava with 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 #19
Source File: Jackson.java    From centraldogma with 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 #20
Source File: Jackson2SockJsMessageCodec.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example #21
Source File: Jackson2SockJsMessageCodec.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected char[] applyJsonQuoting(String content) {
	return JsonStringEncoder.getInstance().quoteAsString(content);
}
 
Example #22
Source File: Utils.java    From tablesaw with 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 File: Utils.java    From tablesaw with 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);
}