Skip to content

Commit

Permalink
Issue #10 IndexOutOfBoundException when passing a string to JsonWrite…
Browse files Browse the repository at this point in the history
…r that contains characters to escape
  • Loading branch information
nmorel committed Jan 14, 2014
1 parent 19829d2 commit 479f5ae
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,13 @@ private void string(String value) throws IOException {
continue;
}
if (last < i) {
out.append(value, last, i - last);
out.append(value, last, i);
}
out.append(replacement);
last = i + 1;
}
if (last < length) {
out.append(value, last, length - last);
out.append(value, last, length);
}
out.append("\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package com.github.nmorel.gwtjackson.client.annotations;

import com.github.nmorel.gwtjackson.client.GwtJacksonTestCase;
import com.github.nmorel.gwtjackson.client.ObjectMapper;
import com.github.nmorel.gwtjackson.client.ObjectWriter;
import com.github.nmorel.gwtjackson.shared.ObjectMapperTester;
import com.github.nmorel.gwtjackson.shared.ObjectWriterTester;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester.ClassGetter;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester.ClassWithJsonAsString;
import com.google.gwt.core.client.GWT;

/**
Expand All @@ -38,6 +41,11 @@ public interface ClassGetterInteger extends ObjectWriter<ClassGetter<Integer>>,
static ClassGetterInteger INSTANCE = GWT.create( ClassGetterInteger.class );
}

public interface ClassWithJsonAsStringMapper extends ObjectMapper<ClassWithJsonAsString>, ObjectMapperTester<ClassWithJsonAsString> {

static ClassWithJsonAsStringMapper INSTANCE = GWT.create( ClassWithJsonAsStringMapper.class );
}

private JsonRawValueTester tester = JsonRawValueTester.INSTANCE;

public void testSimpleStringGetter() {
Expand All @@ -51,4 +59,8 @@ public void testSimpleNonStringGetter() {
public void testNullStringGetter() {
tester.testNullStringGetter( ClassGetterString.INSTANCE );
}

public void testJsonString() {
tester.testJsonString( ClassWithJsonAsStringMapper.INSTANCE );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;

import com.github.nmorel.gwtjackson.client.stream.impl.DefaultJsonWriter;

import com.github.nmorel.gwtjackson.client.GwtJacksonTestCase;
import com.google.gwt.core.client.GWT;

Expand All @@ -31,7 +29,7 @@ public abstract class AbstractJsonWriterTest extends GwtJacksonTestCase {
public abstract JsonWriter newJsonWriter();

public void testWrongTopLevelType() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
try {
jsonWriter.value( "a" );
fail();
Expand All @@ -40,7 +38,7 @@ public void testWrongTopLevelType() throws IOException {
}

public void testTwoNames() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "a" );
try {
Expand All @@ -51,7 +49,7 @@ public void testTwoNames() throws IOException {
}

public void testNameWithoutValue() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "a" );
try {
Expand All @@ -62,7 +60,7 @@ public void testNameWithoutValue() throws IOException {
}

public void testValueWithoutName() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
try {
jsonWriter.value( true );
Expand All @@ -72,7 +70,7 @@ public void testValueWithoutName() throws IOException {
}

public void testMultipleTopLevelValues() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray().endArray();
try {
jsonWriter.beginArray();
Expand All @@ -82,7 +80,7 @@ public void testMultipleTopLevelValues() throws IOException {
}

public void testBadNestingObject() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.beginObject();
try {
Expand All @@ -93,7 +91,7 @@ public void testBadNestingObject() throws IOException {
}

public void testBadNestingArray() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.beginArray();
try {
Expand All @@ -104,7 +102,7 @@ public void testBadNestingArray() throws IOException {
}

public void testNullName() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
try {
jsonWriter.name( null );
Expand All @@ -114,7 +112,7 @@ public void testNullName() throws IOException {
}

public void testNullStringValue() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "a" );
jsonWriter.value( (String) null );
Expand All @@ -123,7 +121,7 @@ public void testNullStringValue() throws IOException {
}

public void testNonFiniteDoubles() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
try {
jsonWriter.value( Double.NaN );
Expand All @@ -143,7 +141,7 @@ public void testNonFiniteDoubles() throws IOException {
}

public void testNonFiniteBoxedDoubles() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
try {
jsonWriter.value( new Double( Double.NaN ) );
Expand All @@ -163,7 +161,7 @@ public void testNonFiniteBoxedDoubles() throws IOException {
}

public void testDoubles() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( -0.0 );
jsonWriter.value( 1.0 );
Expand All @@ -188,7 +186,7 @@ public void testDoubles() throws IOException {
}

public void testLongs() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( 0 );
jsonWriter.value( 1 );
Expand All @@ -201,7 +199,7 @@ public void testLongs() throws IOException {
}

public void testNumbers() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( new BigInteger( "0" ) );
jsonWriter.value( new BigInteger( "9223372036854775808" ) );
Expand All @@ -213,7 +211,7 @@ public void testNumbers() throws IOException {
}

public void testBooleans() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( true );
jsonWriter.value( false );
Expand All @@ -222,15 +220,15 @@ public void testBooleans() throws IOException {
}

public void testNulls() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.nullValue();
jsonWriter.endArray();
assertEquals( "[null]", jsonWriter.getOutput() );
}

public void testStrings() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( "a" );
jsonWriter.value( "a\"" );
Expand All @@ -253,33 +251,34 @@ public void testStrings() throws IOException {
jsonWriter.endArray();
assertEquals( "[\"a\"," + "\"a\\\"\"," + "\"\\\"\"," + "\":\"," + "\",\"," + "\"\\b\"," + "\"\\f\"," + "\"\\n\"," + "\"\\r\"," +
"" + "\"\\t\"," + "\" \"," + "\"\\\\\"," + "\"{\"," + "\"}\"," + "\"[\"," + "\"]\"," + "\"\\u0000\"," + "\"\\u0019\"]",
jsonWriter.getOutput() );
jsonWriter
.getOutput() );
}

public void testUnicodeLineBreaksEscaped() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.value( "\u2028\u2029" );
jsonWriter.endArray();
assertEquals( "[\"\\u2028\\u2029\"]", jsonWriter.getOutput() );
}

public void testEmptyArray() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.endArray();
assertEquals( "[]", jsonWriter.getOutput() );
}

public void testEmptyObject() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.endObject();
assertEquals( "{}", jsonWriter.getOutput() );
}

public void testObjectsInArrays() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginArray();
jsonWriter.beginObject();
jsonWriter.name( "a" ).value( 5 );
Expand All @@ -294,7 +293,7 @@ public void testObjectsInArrays() throws IOException {
}

public void testArraysInObjects() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "a" );
jsonWriter.beginArray();
Expand All @@ -311,7 +310,7 @@ public void testArraysInObjects() throws IOException {
}

public void testDeepNestingArrays() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
for ( int i = 0; i < 20; i++ ) {
jsonWriter.beginArray();
}
Expand All @@ -322,7 +321,7 @@ public void testDeepNestingArrays() throws IOException {
}

public void testDeepNestingObjects() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
for ( int i = 0; i < 20; i++ ) {
jsonWriter.name( "a" );
Expand All @@ -333,11 +332,12 @@ public void testDeepNestingObjects() throws IOException {
}
jsonWriter.endObject();
assertEquals( "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":" +
"{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{" + "}}}}}}}}}}}}}}}}}}}}}", jsonWriter.getOutput() );
"{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{" + "}}}}}}}}}}}}}}}}}}}}}", jsonWriter
.getOutput() );
}

public void testRepeatedName() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "a" ).value( true );
jsonWriter.name( "a" ).value( false );
Expand All @@ -347,7 +347,7 @@ public void testRepeatedName() throws IOException {
}

public void testPrettyPrintObject() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.setIndent( " " );

jsonWriter.beginObject();
Expand All @@ -372,7 +372,7 @@ public void testPrettyPrintObject() throws IOException {
}

public void testPrettyPrintArray() throws IOException {
JsonWriter jsonWriter = newJsonWriter( );
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.setIndent( " " );

jsonWriter.beginArray();
Expand Down Expand Up @@ -487,4 +487,15 @@ public void testWriterCloseIsIdempotent() throws IOException {
jsonWriter.close();
jsonWriter.close();
}

public void testEscaping() throws IOException {
JsonWriter jsonWriter = newJsonWriter();
jsonWriter.beginObject();
jsonWriter.name( "json" );
jsonWriter.value( "{\"key\":\"value\"}" );
jsonWriter.endObject();
jsonWriter.close();

assertEquals( "{\"json\":\"{\\\"key\\\":\\\"value\\\"}\"}", jsonWriter.getOutput() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.github.nmorel.gwtjackson.jackson.AbstractJacksonTest;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester.ClassGetter;
import com.github.nmorel.gwtjackson.shared.annotations.JsonRawValueTester.ClassWithJsonAsString;
import org.junit.Test;

/**
Expand All @@ -41,4 +42,9 @@ public void testSimpleNonStringGetter() {
public void testNullStringGetter() {
JsonRawValueTester.INSTANCE.testNullStringGetter( createMapper( new TypeReference<ClassGetter<String>>() {} ) );
}

@Test
public void testJsonString() {
JsonRawValueTester.INSTANCE.testJsonString( createMapper( new TypeReference<ClassWithJsonAsString>() {} ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package com.github.nmorel.gwtjackson.shared.annotations;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.github.nmorel.gwtjackson.shared.AbstractTester;
import com.github.nmorel.gwtjackson.shared.ObjectMapperTester;
import com.github.nmorel.gwtjackson.shared.ObjectWriterTester;

/**
Expand Down Expand Up @@ -53,6 +55,20 @@ public final static class ClassGetter<T> {
protected T value() { return _value; }
}

public final static class ClassWithJsonAsString {

public String json;

@JsonRawValue
public String jsonRaw;

@JsonCreator
public ClassWithJsonAsString( @JsonProperty("json") String json, @JsonProperty( "jsonRaw" ) String jsonRaw ) {
this.json = json;
this.jsonRaw = jsonRaw;
}
}

public static final JsonRawValueTester INSTANCE = new JsonRawValueTester();

private JsonRawValueTester() {
Expand All @@ -77,4 +93,14 @@ public void testNullStringGetter( ObjectWriterTester<ClassGetter<String>> writer
String result = writer.write( new ClassGetter<String>( null ) );
assertEquals( "{\"nonRaw\":null,\"raw\":null,\"value\":null}", result );
}

public void testJsonString( ObjectMapperTester<ClassWithJsonAsString> mapper ) {
String json = "{\"key\":\"value\"}";
String result = mapper.write( new ClassWithJsonAsString( json, json ) );
assertEquals( "{\"json\":\"{\\\"key\\\":\\\"value\\\"}\",\"jsonRaw\":{\"key\":\"value\"}}", result );

ClassWithJsonAsString bean = mapper.read( "{\"json\":\"{\\\"key\\\":\\\"value\\\"}\"}" );
assertEquals( bean.json, json );
assertNull( bean.jsonRaw );
}
}

0 comments on commit 479f5ae

Please sign in to comment.