Skip to content

Commit

Permalink
Add JsonGenerator.writeRawValue(SerializableString), matching tests…
Browse files Browse the repository at this point in the history
…; planned to help with FasterXML#165
  • Loading branch information
cowtowncoder committed Nov 8, 2014
1 parent 37cdc35 commit 7df7c57
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version: 2.5.0 (xx-xxx-2014)
data format modules)
- Added `ResolvedType.getParameterSource()` to support better resolution
of generic types.
- Added `JsonGenerator.writeRawValue(SerializableString)`

------------------------------------------------------------------------
=== History: ===
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,13 @@ public abstract void writeRawUTF8String(byte[] text, int offset, int length)
*/
public abstract void writeUTF8String(byte[] text, int offset, int length)
throws IOException;

/*
/**********************************************************
/* Public API, write methods, binary/raw content
/**********************************************************
*/

/**
* Method that will force generator to copy
* input text verbatim with <b>no</b> modifications (including
Expand Down Expand Up @@ -817,7 +817,7 @@ public abstract void writeUTF8String(byte[] text, int offset, int length)
public void writeRaw(SerializableString raw) throws IOException {
writeRaw(raw.getValue());
}

/**
* Method that will force generator to copy
* input text verbatim without any modifications, but assuming
Expand All @@ -832,6 +832,17 @@ public void writeRaw(SerializableString raw) throws IOException {

public abstract void writeRawValue(char[] text, int offset, int len) throws IOException;

/**
* Method similar to {@link #writeRawValue(String)}, but potentially more
* efficient as it may be able to use pre-encoded content (similar to
* {@link #writeRaw(SerializableString)}.
*
* @since 2.5
*/
public void writeRawValue(SerializableString raw) throws IOException {
writeRawValue(raw.getValue());
}

/**
* Method that will output given chunk of binary data as base64
* encoded, as a complete String value (surrounded by double quotes).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public JsonGenerator disable(Feature f) {
public void writeString(SerializableString text) throws IOException {
writeString(text.getValue());
}

@Override public void writeRawValue(String text) throws IOException {
_verifyValueWrite("write raw value");
writeRaw(text);
Expand All @@ -204,6 +204,11 @@ public void writeString(SerializableString text) throws IOException {
writeRaw(text, offset, len);
}

@Override public void writeRawValue(SerializableString text) throws IOException {
_verifyValueWrite("write raw value");
writeRaw(text);
}

@Override
public int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) throws IOException {
// Let's implement this as "unsupported" to make it easier to add new parser impls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public void writeUTF8String(byte[] text, int offset, int len) throws IOException
}
_outputBuffer[_outputTail++] = BYTE_QUOTE;
}

/*
/**********************************************************
/* Output method implementations, unprocessed ("raw")
Expand Down Expand Up @@ -591,7 +591,17 @@ public void writeRaw(SerializableString text) throws IOException, JsonGeneration
_writeBytes(raw);
}
}


// since 2.5
@Override
public void writeRawValue(SerializableString text) throws IOException {
_verifyValueWrite("write raw value");
byte[] raw = text.asUnquotedUTF8();
if (raw.length > 0) {
_writeBytes(raw);
}
}

// @TODO: rewrite for speed...
@Override
public final void writeRaw(char[] cbuf, int offset, int len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import static org.junit.Assert.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SerializedString;

/**
* @since 1.7
*/
public class TestRawStringWriting extends com.fasterxml.jackson.core.BaseTest
{
/**
Expand Down Expand Up @@ -79,6 +77,47 @@ public void testUtf8StringsWithEscaping() throws Exception
assertToken(JsonToken.END_ARRAY, jp.nextToken());
jp.close();
}

public void testWriteRawWithSerializable() throws Exception
{
JsonFactory jf = new JsonFactory();

_testWithRaw(jf, true);
_testWithRaw(jf, false);
}

private void _testWithRaw(JsonFactory f, boolean useBytes) throws Exception
{
JsonGenerator jgen;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
StringWriter sw = new StringWriter();

if (useBytes) {
jgen = f.createGenerator(bytes, JsonEncoding.UTF8);
} else {
jgen = f.createGenerator(sw);
}

jgen.writeStartArray();
jgen.writeRawValue(new SerializedString("\"foo\""));
jgen.writeRawValue(new SerializedString("12"));
jgen.writeRaw(new SerializedString(", false"));
jgen.writeEndArray();
jgen.close();

JsonParser p = useBytes
? f.createParser(bytes.toByteArray())
: f.createParser(sw.toString());

assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("foo", p.getText());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(12, p.getIntValue());
assertToken(JsonToken.VALUE_FALSE, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}

/*
/**********************************************************
Expand Down

0 comments on commit 7df7c57

Please sign in to comment.