Skip to content

Commit

Permalink
Fix #60
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 22, 2019
1 parent fab0c6c commit cdd85df
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 29 deletions.
55 changes: 43 additions & 12 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 @@ -898,22 +898,22 @@ public <T> T[] arrayOfFrom(Class<T> type, Object source) throws IOException, JSO
}

@SuppressWarnings("unchecked")
public <T> Map<T,Object> mapFrom(Object source) throws IOException, JSONObjectException
public Map<String,Object> mapFrom(Object source) throws IOException, JSONObjectException
{
if (source instanceof JsonParser) {
JsonParser p = _initForReading((JsonParser) source);
Map<Object,Object> result = _readerForOperation(p).readMap();
Map<?,?> result = _readerForOperation(p).readMap();
p.clearCurrentToken();
return (Map<T,Object>) result;
return (Map<String,Object>) result;
}
JsonParser p = _parser(source);
try {
_initForReading(_config(p));
Map<Object,Object> result = _readerForOperation(p).readMap();
Map<?,?> result = _readerForOperation(p).readMap();
JsonParser p0 = p;
p = null;
_close(p0, null);
return (Map<T,Object>) result;
return (Map<String,Object>) result;
} catch (Exception e) {
return _closeWithError(p, e);
}
Expand Down Expand Up @@ -987,7 +987,7 @@ public Object anyFrom(Object source) throws IOException
* @since 2.8
*/
@SuppressWarnings("unchecked")
public <T extends TreeNode> TreeNode treeFrom(Object source)
public <T extends TreeNode> T treeFrom(Object source)
throws IOException, JSONObjectException
{
if (_treeCodec == null) {
Expand Down Expand Up @@ -1028,7 +1028,7 @@ public <T extends TreeNode> TreeNode treeFrom(Object source)
* @since 2.10
*/
public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source)
throws IOException, JSONObjectException
throws IOException, JSONObjectException
{
JsonParser p;
final boolean managed = !(source instanceof JsonParser);
Expand All @@ -1040,19 +1040,21 @@ public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source)
}
p = _initForReading(_config(p));
JSONReader reader = _readerForOperation(p);
return new ValueIterator<T>(ValueIterator.MODE_BEAN, type, p, reader, managed);
return new ValueIterator<T>(ValueIterator.MODE_BEAN, type,
p, reader, _treeCodec, managed);
}

/**
* Method for creating {@link ValueIterator} for reading
* <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a>
* content (specifically line-delimited and concatenated variants);
* individual values are bound as "Any" type: {@link java.util.Map},
* individual values are bound as "Simple" type: {@link java.util.Map},
* {@link java.util.List}, {@link String}, {@link Number} or {@link Boolean}.
*
* @since 2.10
*/
public ValueIterator<Object> anySequenceFrom(Object source) throws IOException
public ValueIterator<Object> anySequenceFrom(Object source)
throws IOException, JSONObjectException
{
JsonParser p;
final boolean managed = !(source instanceof JsonParser);
Expand All @@ -1064,9 +1066,38 @@ public ValueIterator<Object> anySequenceFrom(Object source) throws IOException
}
p = _initForReading(_config(p));
JSONReader reader = _readerForOperation(p);
return new ValueIterator<Object>(ValueIterator.MODE_ANY, Object.class, p, reader, managed);
return new ValueIterator<Object>(ValueIterator.MODE_ANY, Object.class,
p, reader, _treeCodec, managed);
}


/**
* Method for creating {@link ValueIterator} for reading
* <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a>
* content (specifically line-delimited and concatenated variants);
* individual values are bound as JSON Trees(of type that configured
* {@link TreeCodec}, see {@link #with(TreeCodec)}) supports.
*/
public <T extends TreeNode> ValueIterator<T> treeSequenceFrom(Object source)
throws IOException, JSONObjectException
{
if (_treeCodec == null) {
_noTreeCodec("read TreeNode");
}

JsonParser p;
final boolean managed = !(source instanceof JsonParser);

if (managed) {
p = _parser(source);
} else {
p = (JsonParser) source;
}
p = _initForReading(_config(p));
JSONReader reader = _readerForOperation(p);
return new ValueIterator<T>(ValueIterator.MODE_TREE, TreeNode.class,
p, reader, _treeCodec, managed);
}

/*
/**********************************************************************
/* API: TreeNode construction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
*/
protected final static int MODE_ANY = 2;

/**
* Mode in which values are read as "Tree" values, as bound
* by registered {@link TreeCodec}.
*/
protected final static int MODE_TREE = 3;

protected final static ValueIterator<?> EMPTY_ITERATOR =
new ValueIterator<Object>(MODE_BEAN, null, null, null, false);
new ValueIterator<Object>(MODE_BEAN, null, null, null, null, false);

/*
/**********************************************************************
Expand Down Expand Up @@ -81,6 +87,11 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
*/
protected final JSONReader _reader;

/**
* If "Tree" values are read, codec we need to use for binding
*/
protected final TreeCodec _treeCodec;

/**
* Underlying parser used for reading content to bind. Initialized
* as not <code>null</code> but set as <code>null</code> when
Expand Down Expand Up @@ -125,12 +136,13 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
* closed by iterator.
*/
protected ValueIterator(int mode, Class<?> type, JsonParser p, JSONReader reader,
boolean managedParser)
TreeCodec treeCodec, boolean managedParser)
{
_mode = mode;
_type = type;
_parser = p;
_reader = reader;
_treeCodec = treeCodec;
_closeParser = managedParser;

/* Ok: one more thing; we may have to skip START_ARRAY, assuming
Expand Down Expand Up @@ -280,6 +292,9 @@ public T nextValue() throws IOException
case MODE_ANY:
value = _reader.readValue();
break;
case MODE_TREE:
value = _treeCodec.readTree(_parser);
break;
default:
throw new IllegalStateException("Invalid mode: "+_mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;

import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;

Expand Down Expand Up @@ -53,7 +52,7 @@ public JrsValue get(int i) {
}

@Override
public TreeNode get(String s) {
public JrsValue get(String s) {
return null;
}

Expand All @@ -63,7 +62,7 @@ public JrsValue path(int i){
}

@Override
public TreeNode path(String s) {
public JrsValue path(String s) {
return JrsMissing.instance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;

/**
* Virtual node used instead of `null`, when an operation does not match an
Expand Down Expand Up @@ -51,22 +50,22 @@ public int size() {
}

@Override
public TreeNode get(String s) {
public JrsValue get(String s) {
return null;
}

@Override
public TreeNode get(int i) {
public JrsValue get(int i) {
return null;
}

@Override
public TreeNode path(String s) {
public JrsValue path(String s) {
return this;
}

@Override
public TreeNode path(int i) {
public JrsValue path(int i) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;

public class JrsObject extends JrsValue
{
Expand Down Expand Up @@ -54,7 +53,7 @@ public Iterator<String> fieldNames()
}

@Override
public TreeNode get(int i) {
public JrsValue get(int i) {
return null;
}

Expand All @@ -64,7 +63,7 @@ public JrsValue get(String name) {
}

@Override
public TreeNode path(int i) {
public JrsValue path(int i) {
return JrsMissing.instance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public String asText() {
/**********************************************************************
*/

@Override
public abstract JrsValue get(String s);

@Override
public abstract JrsValue get(int i);

@Override
public abstract JrsValue path(String s);

@Override
public abstract JrsValue path(int i);

protected abstract JrsValue _at(JsonPointer ptr);

protected abstract void write(JsonGenerator g, JacksonJrsTreeCodec codec) throws IOException;
Expand Down Expand Up @@ -138,22 +150,22 @@ public int size() {
}

@Override
public TreeNode get(String s) {
public JrsValue get(String s) {
return null;
}

@Override
public TreeNode get(int i) {
public JrsValue get(int i) {
return null;
}

@Override
public TreeNode path(String s) {
public JrsValue path(String s) {
return JrsMissing.instance();
}

@Override
public TreeNode path(int i) {
public JrsValue path(int i) {
return JrsMissing.instance();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fasterxml.jackson.jr.stree;

import java.io.StringReader;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.TreeCodec;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.ValueIterator;

public class ReadTreeSequencesTest extends TestBase
{
private final TreeCodec TREE_CODEC = new JacksonJrsTreeCodec();
private final JSON treeJSON = JSON.std.with(TREE_CODEC);

public void testBeanSequence() throws Exception
{
final String INPUT = aposToQuotes("{'id':1, 'msg':'foo'} [1, 2, 3] null ");

// First, managed
ValueIterator<JrsValue> it = treeJSON.treeSequenceFrom(INPUT);
_verifyTreeSequence(it);
it.close();

// and parser we create
JsonParser p = treeJSON.createParser(new StringReader(INPUT));

it = treeJSON.treeSequenceFrom(p);
_verifyTreeSequence(it);
it.close();
p.close();
}

private void _verifyTreeSequence(ValueIterator<JrsValue> it) throws Exception
{
assertTrue(it.hasNext());
JrsValue tree = it.nextValue();
assertTrue(tree.isObject());
assertEquals(2, tree.size());
assertEquals(NumberType.INT, tree.path("id").numberType());
assertEquals("foo", tree.path("msg").asText());

assertTrue(it.hasNext());
tree = it.nextValue();
assertTrue(tree.isArray());
assertEquals(3, tree.size());
assertTrue(tree.get(0).isNumber());
assertTrue(tree.get(1).isNumber());
assertEquals(NumberType.INT, tree.get(2).numberType());

assertTrue(it.hasNext());
assertNull(it.nextValue());

assertFalse(it.hasNext());
}
}

0 comments on commit cdd85df

Please sign in to comment.