Skip to content

Commit

Permalink
Fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 16, 2016
1 parent 3a3e038 commit 2282803
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 59 deletions.
61 changes: 30 additions & 31 deletions jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,11 @@ public byte[] asBytes(Object value) throws IOException, JSONObjectException
return result;
}

public void write(Object value, JsonGenerator jgen) throws IOException, JSONObjectException {
public void write(Object value, JsonGenerator gen) throws IOException, JSONObjectException {
// NOTE: no call to _config(); assumed to be fully configured
_writerForOperation(jgen).writeValue(value);
_writerForOperation(gen).writeValue(value);
if (Feature.FLUSH_AFTER_WRITE_VALUE.isEnabled(_features)) {
jgen.flush();
gen.flush();
}
}

Expand Down Expand Up @@ -653,32 +653,32 @@ public JSONComposer<OutputStream> composeUsing(JsonGenerator gen) throws IOExcep

public JSONComposer<OutputStream> composeTo(OutputStream out) throws IOException, JSONObjectException {
return JSONComposer.streamComposer(_features,
_jsonFactory.createGenerator(out), true);
_config(_jsonFactory.createGenerator(out)), true);
}

public JSONComposer<OutputStream> composeTo(Writer w) throws IOException, JSONObjectException {
return JSONComposer.streamComposer(_features,
_jsonFactory.createGenerator(w), true);
_config(_jsonFactory.createGenerator(w)), true);
}

public JSONComposer<OutputStream> composeTo(File f) throws IOException, JSONObjectException {
return JSONComposer.streamComposer(_features,
_jsonFactory.createGenerator(f, JsonEncoding.UTF8), true);
_config(_jsonFactory.createGenerator(f, JsonEncoding.UTF8)), true);
}

@SuppressWarnings("resource")
public JSONComposer<String> composeString() throws IOException, JSONObjectException {
SegmentedStringWriter out = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
JsonGenerator gen = _jsonFactory.createGenerator(out)
.setCodec(asCodec());
JsonGenerator gen = _config(_jsonFactory.createGenerator(out)
.setCodec(asCodec()));
return JSONComposer.stringComposer(_features, gen, out);
}

@SuppressWarnings("resource")
public JSONComposer<byte[]> composeBytes() throws IOException, JSONObjectException {
ByteArrayBuilder out = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
JsonGenerator gen = _jsonFactory.createGenerator(out)
.setCodec(asCodec());
JsonGenerator gen = _config(_jsonFactory.createGenerator(out)
.setCodec(asCodec()));
return JSONComposer.bytesComposer(_features, gen, out);
}

Expand Down Expand Up @@ -866,7 +866,7 @@ public <T> T beanFrom(Class<T> type, Object source) throws IOException, JSONObje
*</ul>
*/
@SuppressWarnings("resource")
public Object anyFrom(Object source) throws IOException, JSONObjectException
public Object anyFrom(Object source) throws IOException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
Expand Down Expand Up @@ -894,27 +894,27 @@ public Object anyFrom(Object source) throws IOException, JSONObjectException
/**********************************************************************
*/

protected final void _writeAndClose(Object value, JsonGenerator jgen)
throws IOException, JSONObjectException
protected final void _writeAndClose(Object value, JsonGenerator g)
throws IOException
{
boolean closed = false;
try {
_config(jgen);
_writerForOperation(jgen).writeValue(value);
_config(g);
_writerForOperation(g).writeValue(value);
closed = true;
jgen.close();
g.close();
} finally {
if (!closed) {
// need to catch possible failure, so as not to mask problem
try {
jgen.close();
g.close();
} catch (IOException ioe) { }
}
}
}

protected JSONWriter _writerForOperation(JsonGenerator jgen) {
return _writer.perOperationInstance(jgen);
protected JSONWriter _writerForOperation(JsonGenerator gen) {
return _writer.perOperationInstance(gen);
}

/*
Expand All @@ -923,8 +923,8 @@ protected JSONWriter _writerForOperation(JsonGenerator jgen) {
/**********************************************************************
*/

protected JSONReader _readerForOperation(JsonParser jp) {
return _reader.perOperationInstance(jp);
protected JSONReader _readerForOperation(JsonParser p) {
return _reader.perOperationInstance(p);
}

protected JsonParser _parser(Object source) throws IOException, JSONObjectException
Expand Down Expand Up @@ -959,21 +959,20 @@ protected JsonParser _parser(Object source) throws IOException, JSONObjectExcept
+" as input (use an InputStream, Reader, String, byte[], File or URL");
}

protected JsonParser _initForReading(JsonParser jp)
throws IOException, JsonProcessingException
protected JsonParser _initForReading(JsonParser p) throws IOException
{
/* First: must point to a token; if not pointing to one, advance.
* This occurs before first read from JsonParser, as well as
* after clearing of current token.
*/
JsonToken t = jp.getCurrentToken();
JsonToken t = p.getCurrentToken();
if (t == null) { // and then we must get something...
t = jp.nextToken();
t = p.nextToken();
if (t == null) { // not cool is it?
throw JSONObjectException.from(jp, "No content to map due to end-of-input");
throw JSONObjectException.from(p, "No content to map due to end-of-input");
}
}
return jp;
return p;
}

/*
Expand All @@ -982,19 +981,19 @@ protected JsonParser _initForReading(JsonParser jp)
/**********************************************************************
*/

protected JsonGenerator _config(JsonGenerator jgen)
protected JsonGenerator _config(JsonGenerator g)
{
// First, possible pretty printing
PrettyPrinter pp = _prettyPrinter;
if (pp != null) {
if (pp instanceof Instantiatable<?>) {
pp = (PrettyPrinter) ((Instantiatable<?>) pp).createInstance();
}
jgen.setPrettyPrinter(pp);
g.setPrettyPrinter(pp);
} else if (isEnabled(Feature.PRETTY_PRINT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
g.useDefaultPrettyPrinter();
}
return jgen;
return g;
}

protected JsonParser _config(JsonParser jp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;

import com.fasterxml.jackson.jr.ob.JSON.Feature;
import com.fasterxml.jackson.jr.ob.comp.SequenceComposer;

Expand Down Expand Up @@ -112,7 +112,7 @@ public static JSONComposer<Map<String,Object>> mapComposer(int features,
* and return instance of specified result type.
*/
@SuppressWarnings("unchecked")
public T finish() throws IOException, JsonProcessingException
public T finish() throws IOException
{
if (_open) {
_closeChild();
Expand Down Expand Up @@ -146,13 +146,13 @@ public T finish() throws IOException, JsonProcessingException
*/

@Override
protected JSONComposer<T> _start() throws IOException, JsonProcessingException {
protected JSONComposer<T> _start() throws IOException {
// Should never be called
throw _illegalCall();
}

@Override
protected Object _finish() throws IOException, JsonProcessingException {
protected Object _finish() throws IOException {
// Should never be called
throw _illegalCall();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;

public abstract class SequenceComposer<THIS extends SequenceComposer<THIS>>
extends ComposerBase
Expand Down Expand Up @@ -37,15 +36,13 @@ public void flush() throws IOException {
/**********************************************************************
*/

public ArrayComposer<THIS> startArray()
throws IOException, JsonProcessingException
public ArrayComposer<THIS> startArray() throws IOException
{
_closeChild();
return _startArray(_this(), _generator);
}

public ObjectComposer<THIS> startObject()
throws IOException, JsonProcessingException
public ObjectComposer<THIS> startObject() throws IOException
{
_closeChild();
return _startObject(_this(), _generator);
Expand All @@ -57,22 +54,19 @@ public ObjectComposer<THIS> startObject()
/**********************************************************************
*/

public THIS add(int value)
throws IOException, JsonProcessingException
public THIS add(int value) throws IOException
{
_generator.writeNumber(value);
return _this();
}

public THIS add(long value)
throws IOException, JsonProcessingException
public THIS add(long value) throws IOException
{
_generator.writeNumber(value);
return _this();
}

public THIS add(double value)
throws IOException, JsonProcessingException
public THIS add(double value) throws IOException
{
_generator.writeNumber(value);
return _this();
Expand All @@ -84,15 +78,13 @@ public THIS add(double value)
/**********************************************************************
*/

public THIS add(String value)
throws IOException, JsonProcessingException
public THIS add(String value) throws IOException
{
_generator.writeString(value);
return _this();
}

public THIS add(CharSequence value)
throws IOException, JsonProcessingException
public THIS add(CharSequence value) throws IOException
{
String str = (value == null) ? null : value.toString();
_generator.writeString(str);
Expand All @@ -105,15 +97,13 @@ public THIS add(CharSequence value)
/**********************************************************************
*/

public THIS addNull()
throws IOException, JsonProcessingException
public THIS addNull() throws IOException
{
_generator.writeNull();
return _this();
}

public THIS add(boolean value)
throws IOException, JsonProcessingException
public THIS add(boolean value) throws IOException
{
_generator.writeBoolean(value);
return _this();
Expand All @@ -125,8 +115,7 @@ public THIS add(boolean value)
* has a properly configure {@link com.fasterxml.jackson.core.ObjectCodec}
* to use for serializer object.
*/
public THIS addObject(Object pojo)
throws IOException, JsonProcessingException
public THIS addObject(Object pojo) throws IOException
{
_generator.writeObject(pojo);
return _this();
Expand All @@ -138,8 +127,7 @@ public THIS addObject(Object pojo)
/**********************************************************************
*/

protected void _closeChild()
throws IOException, JsonProcessingException
protected void _closeChild() throws IOException
{
if (_child != null) {
_child._finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ public void testSimpleList() throws Exception
+"}",
JSON.std.with(JSON.Feature.PRETTY_PRINT_OUTPUT).asString(map));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,19 @@ public void testComposerWithPojo() throws Exception
.finish();
assertEquals(aposToQuotes("[{'first':'Bob'},{'name':{'first':'Bill'}}]"), json);
}

public void testComposerWithIndent() throws Exception
{
String json = JSON.std
.with(JSON.Feature.PRETTY_PRINT_OUTPUT)
.composeString()
.startObject()
.put("name", "Bill")
.end()
.finish();
assertEquals(aposToQuotes("{\n"
+" 'name' : 'Bill'\n"
+"}"),
json);
}
}
4 changes: 4 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Project: jackson-jr
------------------------------------------------------------------------

2.7.3 (16-Mar-2016)

#38: PRETTY_PRINT_OUTPUT with composer doesn't work
(reported by weizhu-us@github)

2.7.2 (26-Feb-2016)
2.7.1 (02-Feb-2016)

Expand Down

0 comments on commit 2282803

Please sign in to comment.