Skip to content

Commit

Permalink
First sort-of working version of #549
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 12, 2019
1 parent 2b19c0b commit 56c4371
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOExc
protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
{
WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
_generatorFeatures, _objectCodec, out);
_generatorFeatures, _objectCodec, out, _quoteChar);
if (_maximumNonEscapedChar > 0) {
gen.setHighestNonEscapedChar(_maximumNonEscapedChar);
}
Expand All @@ -1593,7 +1593,7 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
*/
protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
_generatorFeatures, _objectCodec, out);
_generatorFeatures, _objectCodec, out, _quoteChar);
if (_maximumNonEscapedChar > 0) {
gen.setHighestNonEscapedChar(_maximumNonEscapedChar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ public class UTF8JsonGenerator
/**********************************************************
*/

/**
* @since 2.10
*/
@SuppressWarnings("deprecation")
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
OutputStream out)
OutputStream out, char quoteChar)
{
super(ctxt, features, codec);
_outputStream = out;
_quoteChar = (byte) quoteChar; // TODO: validate/truncate

_bufferRecyclable = true;
_outputBuffer = ctxt.allocWriteEncodingBuffer();
_outputEnd = _outputBuffer.length;
Expand All @@ -137,13 +142,18 @@ public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
}
}

/**
* @since 2.10
*/
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
OutputStream out,
OutputStream out, char quoteChar,
byte[] outputBuffer, int outputOffset, boolean bufferRecyclable)
{

super(ctxt, features, codec);
_outputStream = out;
_quoteChar = (byte) quoteChar; // TODO: validate/truncate

_bufferRecyclable = bufferRecyclable;
_outputTail = outputOffset;
_outputBuffer = outputBuffer;
Expand All @@ -154,12 +164,27 @@ public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
_charBufferLength = _charBuffer.length;
}

@Deprecated // since 2.10
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
OutputStream out) {
this(ctxt, features, codec, out, JsonFactory.DEFAULT_QUOTE_CHAR);
}

@Deprecated // since 2.10
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
OutputStream out,
byte[] outputBuffer, int outputOffset, boolean bufferRecyclable)
{
this(ctxt, features, codec, out, JsonFactory.DEFAULT_QUOTE_CHAR,
outputBuffer, outputOffset, bufferRecyclable);
}

/*
/**********************************************************
/* Overridden configuration methods
/**********************************************************
*/

@Override
public Object getOutputTarget() {
return _outputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,25 @@ public class WriterBasedJsonGenerator
/**********************************************************
*/

@Deprecated // since 2.10
public WriterBasedJsonGenerator(IOContext ctxt, int features,
ObjectCodec codec, Writer w)
ObjectCodec codec, Writer w) {
this(ctxt, features, codec, w, JsonFactory.DEFAULT_QUOTE_CHAR);
}

/**
* @since 2.10
*/
public WriterBasedJsonGenerator(IOContext ctxt, int features,
ObjectCodec codec, Writer w,
char quoteChar)

{
super(ctxt, features, codec);
_writer = w;
_outputBuffer = ctxt.allocConcatBuffer();
_outputEnd = _outputBuffer.length;
_quoteChar = quoteChar;
}

/*
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/com/fasterxml/jackson/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ protected void verifyIntValue(JsonParser p, long expValue)

/*
/**********************************************************
/* Parser/generator construction
/* Parser construction
/**********************************************************
*/

Expand Down Expand Up @@ -396,12 +396,34 @@ protected JsonParser createParserForDataInput(JsonFactory f,
return f.createParser(input);
}

/*
/**********************************************************
/* Generator construction
/**********************************************************
*/

protected JsonGenerator createGenerator(OutputStream out) throws IOException {
return createGenerator(JSON_FACTORY, out);
}

protected JsonGenerator createGenerator(TokenStreamFactory f, OutputStream out) throws IOException {
return f.createGenerator(out);
}

protected JsonGenerator createGenerator(Writer w) throws IOException {
return createGenerator(JSON_FACTORY, w);
}

protected JsonGenerator createGenerator(TokenStreamFactory f, Writer w) throws IOException {
return f.createGenerator(w);
}

/*
/**********************************************************
/* Helper read/write methods
/**********************************************************
*/

protected void writeJsonDoc(JsonFactory f, String doc, Writer w) throws IOException
{
writeJsonDoc(f, doc, f.createGenerator(w));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/fasterxml/jackson/core/TestVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void testCoreVersions() throws Exception
CharsToNameCanonicalizer.createRoot());
assertVersion(jp.version());
jp.close();
JsonGenerator jgen = new WriterBasedJsonGenerator(getIOContext(), 0, null, null);
JsonGenerator jgen = new WriterBasedJsonGenerator(getIOContext(), 0, null, null, '"');
assertVersion(jgen.version());
jgen.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.fasterxml.jackson.core.json;

import java.io.*;

import com.fasterxml.jackson.core.*;

// For [core#549], ability to use alternate quote characters
public class CustomQuoteCharTest
extends com.fasterxml.jackson.core.BaseTest
{
final JsonFactory JSON_F = streamFactoryBuilder()
.quoteChar('\'')
.build();

public void testBasicAposWithCharBased() throws Exception
{
StringWriter w;
JsonGenerator g;

// with Object
w = new StringWriter();
g = createGenerator(JSON_F, w);
g.writeStartObject();
g.writeStringField("question", "answer");
g.writeEndObject();
g.close();
assertEquals("{'question':'answer'}", w.toString());

// with Array
w = new StringWriter();
g = createGenerator(JSON_F, w);
g.writeStartArray();
g.writeString("hello world");
g.writeEndArray();
g.close();
assertEquals("['hello world']", w.toString());
}

public void testBasicAposWithByteBased() throws Exception
{
ByteArrayOutputStream out;
JsonGenerator g;

// with Object
out = new ByteArrayOutputStream();
g = createGenerator(JSON_F, out);
g.writeStartObject();
g.writeStringField("question", "answer");
g.writeEndObject();
g.close();
assertEquals("{'question':'answer'}", out.toString("UTF-8"));

// with Array
out = new ByteArrayOutputStream();
g = createGenerator(JSON_F, out);
g.writeStartArray();
g.writeString("hello world");
g.writeEndArray();
g.close();
assertEquals("['hello world']", out.toString("UTF-8"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void testUtf8Issue462() throws Exception
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
IOContext ioc = new IOContext(new BufferRecycler(), bytes, true);
JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes);
JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes, '"');
String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04";
int length = 4000 - 38;

Expand Down

0 comments on commit 56c4371

Please sign in to comment.