Skip to content

Commit

Permalink
Implement first part of #43
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 25, 2016
1 parent 66cb3e0 commit 8360479
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 90 deletions.
201 changes: 113 additions & 88 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 @@ -18,6 +18,7 @@
*<p>
* Note that instances are fully immutable, and thereby thread-safe.
*/
@SuppressWarnings("resource")
public class JSON implements Versioned
{
/**
Expand Down Expand Up @@ -591,7 +592,6 @@ public final boolean isEnabled(Feature f) {
/**********************************************************************
*/

@SuppressWarnings("resource")
public String asString(Object value) throws IOException, JSONObjectException
{
SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
Expand All @@ -605,7 +605,6 @@ public String asString(Object value) throws IOException, JSONObjectException
return sw.getAndClear();
}

@SuppressWarnings("resource")
public byte[] asBytes(Object value) throws IOException, JSONObjectException
{
ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
Expand Down Expand Up @@ -665,16 +664,14 @@ public JSONComposer<OutputStream> composeTo(File f) throws IOException, JSONObje
return JSONComposer.streamComposer(_features,
_config(_jsonFactory.createGenerator(f, JsonEncoding.UTF8)), true);
}

@SuppressWarnings("resource")

public JSONComposer<String> composeString() throws IOException, JSONObjectException {
SegmentedStringWriter out = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
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 = _config(_jsonFactory.createGenerator(out)
Expand All @@ -698,151 +695,146 @@ public MapComposer<?> composeMap() {
public MapComposer<?> composeMap(Map<String,Object> map) {
return new MapComposer<ComposerBase>(map);
}

/*
/**********************************************************************
/* API: reading JSON as Simple Objects
/**********************************************************************
*/

@SuppressWarnings("resource")
public List<Object> listFrom(Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
// note: no call to _config(), should come pre-configured
JsonParser jp = _initForReading((JsonParser) source);
List<Object> result = _readerForOperation(jp).readList();
JsonParser p = _initForReading((JsonParser) source);
List<Object> result = _readerForOperation(p).readList();
// Need to consume the token too
jp.clearCurrentToken();
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
List<Object> result = _readerForOperation(jp).readList();
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
List<Object> result = _readerForOperation(p).readList();
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}

@SuppressWarnings("resource")
public <T> List<T> listOfFrom(Class<T> type, Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
// note: no call to _config(), should come pre-configured
JsonParser jp = _initForReading((JsonParser) source);
List<T> result = _readerForOperation(jp).readListOf(type);
JsonParser p = _initForReading((JsonParser) source);
List<T> result = _readerForOperation(p).readListOf(type);
// Need to consume the token too
jp.clearCurrentToken();
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
List<T> result = _readerForOperation(jp).readListOf(type);
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
List<T> result = _readerForOperation(p).readListOf(type);
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}

@SuppressWarnings("resource")

public Object[] arrayFrom(Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
Object[] result = _readerForOperation(jp).readArray();
jp.clearCurrentToken();
JsonParser p = _initForReading((JsonParser) source);
Object[] result = _readerForOperation(p).readArray();
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
Object[] result = _readerForOperation(jp).readArray();
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
Object[] result = _readerForOperation(p).readArray();
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}

@SuppressWarnings("resource")
public <T> T[] arrayOfFrom(Class<T> type, Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
T[] result = _readerForOperation(jp).readArrayOf(type);
jp.clearCurrentToken();
JsonParser p = _initForReading((JsonParser) source);
T[] result = _readerForOperation(p).readArrayOf(type);
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
T[] result = _readerForOperation(jp).readArrayOf(type);
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
T[] result = _readerForOperation(p).readArrayOf(type);
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}
@SuppressWarnings({ "unchecked", "resource" })

@SuppressWarnings("unchecked")
public <T> Map<T,Object> mapFrom(Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
Map<Object,Object> result = _readerForOperation(jp).readMap();
jp.clearCurrentToken();
JsonParser p = _initForReading((JsonParser) source);
Map<Object,Object> result = _readerForOperation(p).readMap();
p.clearCurrentToken();
return (Map<T,Object>) result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
Map<Object,Object> result = _readerForOperation(jp).readMap();
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
Map<Object,Object> result = _readerForOperation(p).readMap();
JsonParser p0 = p;
p = null;
_close(p0, null);
return (Map<T,Object>) result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}

@SuppressWarnings("resource")
public <T> T beanFrom(Class<T> type, Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
T result = _readerForOperation(jp).readBean(type);
jp.clearCurrentToken();
JsonParser p = _initForReading((JsonParser) source);
T result = _readerForOperation(p).readBean(type);
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
T result = _readerForOperation(jp).readBean(type);
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
T result = _readerForOperation(p).readBean(type);
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}
Expand All @@ -865,29 +857,62 @@ public <T> T beanFrom(Class<T> type, Object source) throws IOException, JSONObje
* <li><code>char[]</code></li>
*</ul>
*/
@SuppressWarnings("resource")
public Object anyFrom(Object source) throws IOException
{
if (source instanceof JsonParser) {
JsonParser jp = _initForReading((JsonParser) source);
Object result = _readerForOperation(jp).readValue();
jp.clearCurrentToken();
JsonParser p = _initForReading((JsonParser) source);
Object result = _readerForOperation(p).readValue();
p.clearCurrentToken();
return result;
}
JsonParser jp = _parser(source);
JsonParser p = _parser(source);
try {
_initForReading(_config(jp));
Object result = _readerForOperation(jp).readValue();
JsonParser jp0 = jp;
jp = null;
_close(jp0, null);
_initForReading(_config(p));
Object result = _readerForOperation(p).readValue();
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(jp, e);
_close(p, e);
return null;
}
}


/**
* Method for reading content as a JSON Tree (of type that configured
* {@link TreeCodec}, see {@link #with(TreeCodec)}) supports.
*
* @since 2.8
*/
@SuppressWarnings("unchecked")
public <T extends TreeNode> TreeNode treeFrom(Object source)
throws IOException, JSONObjectException
{
if (_treeCodec == null) {
throw new IllegalStateException("JSON instance does not have configured TreeCodec to read trees");
}

if (source instanceof JsonParser) {
JsonParser p = _initForReading((JsonParser) source);
T result = (T) _treeCodec.readTree(p);
p.clearCurrentToken();
return result;
}
JsonParser p = _parser(source);
try {
_initForReading(_config(p));
T result = (T) _treeCodec.readTree(p);
JsonParser p0 = p;
p = null;
_close(p0, null);
return result;
} catch (Exception e) {
_close(p, e);
return null;
}
}

/*
/**********************************************************************
/* Internal methods, writing
Expand Down Expand Up @@ -996,10 +1021,10 @@ protected JsonGenerator _config(JsonGenerator g)
return g;
}

protected JsonParser _config(JsonParser jp)
protected JsonParser _config(JsonParser p)
{
// nothing to do, yet
return jp;
return p;
}

protected void _close(Closeable cl) {
Expand Down
9 changes: 9 additions & 0 deletions jr-stree/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ has no other dependencies.
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<!-- Let's actually test with jr-objects
-->
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-objects</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit 8360479

Please sign in to comment.