Skip to content

Commit cdd85df

Browse files
committed
Fix #60
1 parent fab0c6c commit cdd85df

File tree

7 files changed

+140
-29
lines changed

7 files changed

+140
-29
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java

+43-12
Original file line numberDiff line numberDiff line change
@@ -898,22 +898,22 @@ public <T> T[] arrayOfFrom(Class<T> type, Object source) throws IOException, JSO
898898
}
899899

900900
@SuppressWarnings("unchecked")
901-
public <T> Map<T,Object> mapFrom(Object source) throws IOException, JSONObjectException
901+
public Map<String,Object> mapFrom(Object source) throws IOException, JSONObjectException
902902
{
903903
if (source instanceof JsonParser) {
904904
JsonParser p = _initForReading((JsonParser) source);
905-
Map<Object,Object> result = _readerForOperation(p).readMap();
905+
Map<?,?> result = _readerForOperation(p).readMap();
906906
p.clearCurrentToken();
907-
return (Map<T,Object>) result;
907+
return (Map<String,Object>) result;
908908
}
909909
JsonParser p = _parser(source);
910910
try {
911911
_initForReading(_config(p));
912-
Map<Object,Object> result = _readerForOperation(p).readMap();
912+
Map<?,?> result = _readerForOperation(p).readMap();
913913
JsonParser p0 = p;
914914
p = null;
915915
_close(p0, null);
916-
return (Map<T,Object>) result;
916+
return (Map<String,Object>) result;
917917
} catch (Exception e) {
918918
return _closeWithError(p, e);
919919
}
@@ -987,7 +987,7 @@ public Object anyFrom(Object source) throws IOException
987987
* @since 2.8
988988
*/
989989
@SuppressWarnings("unchecked")
990-
public <T extends TreeNode> TreeNode treeFrom(Object source)
990+
public <T extends TreeNode> T treeFrom(Object source)
991991
throws IOException, JSONObjectException
992992
{
993993
if (_treeCodec == null) {
@@ -1028,7 +1028,7 @@ public <T extends TreeNode> TreeNode treeFrom(Object source)
10281028
* @since 2.10
10291029
*/
10301030
public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source)
1031-
throws IOException, JSONObjectException
1031+
throws IOException, JSONObjectException
10321032
{
10331033
JsonParser p;
10341034
final boolean managed = !(source instanceof JsonParser);
@@ -1040,19 +1040,21 @@ public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source)
10401040
}
10411041
p = _initForReading(_config(p));
10421042
JSONReader reader = _readerForOperation(p);
1043-
return new ValueIterator<T>(ValueIterator.MODE_BEAN, type, p, reader, managed);
1043+
return new ValueIterator<T>(ValueIterator.MODE_BEAN, type,
1044+
p, reader, _treeCodec, managed);
10441045
}
10451046

10461047
/**
10471048
* Method for creating {@link ValueIterator} for reading
10481049
* <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a>
10491050
* content (specifically line-delimited and concatenated variants);
1050-
* individual values are bound as "Any" type: {@link java.util.Map},
1051+
* individual values are bound as "Simple" type: {@link java.util.Map},
10511052
* {@link java.util.List}, {@link String}, {@link Number} or {@link Boolean}.
10521053
*
10531054
* @since 2.10
10541055
*/
1055-
public ValueIterator<Object> anySequenceFrom(Object source) throws IOException
1056+
public ValueIterator<Object> anySequenceFrom(Object source)
1057+
throws IOException, JSONObjectException
10561058
{
10571059
JsonParser p;
10581060
final boolean managed = !(source instanceof JsonParser);
@@ -1064,9 +1066,38 @@ public ValueIterator<Object> anySequenceFrom(Object source) throws IOException
10641066
}
10651067
p = _initForReading(_config(p));
10661068
JSONReader reader = _readerForOperation(p);
1067-
return new ValueIterator<Object>(ValueIterator.MODE_ANY, Object.class, p, reader, managed);
1069+
return new ValueIterator<Object>(ValueIterator.MODE_ANY, Object.class,
1070+
p, reader, _treeCodec, managed);
10681071
}
1069-
1072+
1073+
/**
1074+
* Method for creating {@link ValueIterator} for reading
1075+
* <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a>
1076+
* content (specifically line-delimited and concatenated variants);
1077+
* individual values are bound as JSON Trees(of type that configured
1078+
* {@link TreeCodec}, see {@link #with(TreeCodec)}) supports.
1079+
*/
1080+
public <T extends TreeNode> ValueIterator<T> treeSequenceFrom(Object source)
1081+
throws IOException, JSONObjectException
1082+
{
1083+
if (_treeCodec == null) {
1084+
_noTreeCodec("read TreeNode");
1085+
}
1086+
1087+
JsonParser p;
1088+
final boolean managed = !(source instanceof JsonParser);
1089+
1090+
if (managed) {
1091+
p = _parser(source);
1092+
} else {
1093+
p = (JsonParser) source;
1094+
}
1095+
p = _initForReading(_config(p));
1096+
JSONReader reader = _readerForOperation(p);
1097+
return new ValueIterator<T>(ValueIterator.MODE_TREE, TreeNode.class,
1098+
p, reader, _treeCodec, managed);
1099+
}
1100+
10701101
/*
10711102
/**********************************************************************
10721103
/* API: TreeNode construction

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/ValueIterator.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
2929
*/
3030
protected final static int MODE_ANY = 2;
3131

32+
/**
33+
* Mode in which values are read as "Tree" values, as bound
34+
* by registered {@link TreeCodec}.
35+
*/
36+
protected final static int MODE_TREE = 3;
37+
3238
protected final static ValueIterator<?> EMPTY_ITERATOR =
33-
new ValueIterator<Object>(MODE_BEAN, null, null, null, false);
39+
new ValueIterator<Object>(MODE_BEAN, null, null, null, null, false);
3440

3541
/*
3642
/**********************************************************************
@@ -81,6 +87,11 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
8187
*/
8288
protected final JSONReader _reader;
8389

90+
/**
91+
* If "Tree" values are read, codec we need to use for binding
92+
*/
93+
protected final TreeCodec _treeCodec;
94+
8495
/**
8596
* Underlying parser used for reading content to bind. Initialized
8697
* as not <code>null</code> but set as <code>null</code> when
@@ -125,12 +136,13 @@ public class ValueIterator<T> implements Iterator<T>, Closeable
125136
* closed by iterator.
126137
*/
127138
protected ValueIterator(int mode, Class<?> type, JsonParser p, JSONReader reader,
128-
boolean managedParser)
139+
TreeCodec treeCodec, boolean managedParser)
129140
{
130141
_mode = mode;
131142
_type = type;
132143
_parser = p;
133144
_reader = reader;
145+
_treeCodec = treeCodec;
134146
_closeParser = managedParser;
135147

136148
/* Ok: one more thing; we may have to skip START_ARRAY, assuming
@@ -280,6 +292,9 @@ public T nextValue() throws IOException
280292
case MODE_ANY:
281293
value = _reader.readValue();
282294
break;
295+
case MODE_TREE:
296+
value = _treeCodec.readTree(_parser);
297+
break;
283298
default:
284299
throw new IllegalStateException("Invalid mode: "+_mode);
285300
}

jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JrsArray.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.fasterxml.jackson.core.JsonGenerator;
77
import com.fasterxml.jackson.core.JsonPointer;
88
import com.fasterxml.jackson.core.JsonToken;
9-
import com.fasterxml.jackson.core.TreeNode;
109

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

@@ -53,7 +52,7 @@ public JrsValue get(int i) {
5352
}
5453

5554
@Override
56-
public TreeNode get(String s) {
55+
public JrsValue get(String s) {
5756
return null;
5857
}
5958

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

6564
@Override
66-
public TreeNode path(String s) {
65+
public JrsValue path(String s) {
6766
return JrsMissing.instance();
6867
}
6968

jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JrsMissing.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.core.JsonGenerator;
66
import com.fasterxml.jackson.core.JsonPointer;
77
import com.fasterxml.jackson.core.JsonToken;
8-
import com.fasterxml.jackson.core.TreeNode;
98

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

5352
@Override
54-
public TreeNode get(String s) {
53+
public JrsValue get(String s) {
5554
return null;
5655
}
5756

5857
@Override
59-
public TreeNode get(int i) {
58+
public JrsValue get(int i) {
6059
return null;
6160
}
6261

6362
@Override
64-
public TreeNode path(String s) {
63+
public JrsValue path(String s) {
6564
return this;
6665
}
6766

6867
@Override
69-
public TreeNode path(int i) {
68+
public JrsValue path(int i) {
7069
return this;
7170
}
7271

jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JrsObject.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.fasterxml.jackson.core.JsonGenerator;
99
import com.fasterxml.jackson.core.JsonPointer;
1010
import com.fasterxml.jackson.core.JsonToken;
11-
import com.fasterxml.jackson.core.TreeNode;
1211

1312
public class JrsObject extends JrsValue
1413
{
@@ -54,7 +53,7 @@ public Iterator<String> fieldNames()
5453
}
5554

5655
@Override
57-
public TreeNode get(int i) {
56+
public JrsValue get(int i) {
5857
return null;
5958
}
6059

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

6665
@Override
67-
public TreeNode path(int i) {
66+
public JrsValue path(int i) {
6867
return JrsMissing.instance();
6968
}
7069

jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JrsValue.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ public String asText() {
106106
/**********************************************************************
107107
*/
108108

109+
@Override
110+
public abstract JrsValue get(String s);
111+
112+
@Override
113+
public abstract JrsValue get(int i);
114+
115+
@Override
116+
public abstract JrsValue path(String s);
117+
118+
@Override
119+
public abstract JrsValue path(int i);
120+
109121
protected abstract JrsValue _at(JsonPointer ptr);
110122

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

140152
@Override
141-
public TreeNode get(String s) {
153+
public JrsValue get(String s) {
142154
return null;
143155
}
144156

145157
@Override
146-
public TreeNode get(int i) {
158+
public JrsValue get(int i) {
147159
return null;
148160
}
149161

150162
@Override
151-
public TreeNode path(String s) {
163+
public JrsValue path(String s) {
152164
return JrsMissing.instance();
153165
}
154166

155167
@Override
156-
public TreeNode path(int i) {
168+
public JrsValue path(int i) {
157169
return JrsMissing.instance();
158170
}
159171

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fasterxml.jackson.jr.stree;
2+
3+
import java.io.StringReader;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.JsonParser.NumberType;
7+
import com.fasterxml.jackson.core.TreeCodec;
8+
import com.fasterxml.jackson.jr.ob.JSON;
9+
import com.fasterxml.jackson.jr.ob.ValueIterator;
10+
11+
public class ReadTreeSequencesTest extends TestBase
12+
{
13+
private final TreeCodec TREE_CODEC = new JacksonJrsTreeCodec();
14+
private final JSON treeJSON = JSON.std.with(TREE_CODEC);
15+
16+
public void testBeanSequence() throws Exception
17+
{
18+
final String INPUT = aposToQuotes("{'id':1, 'msg':'foo'} [1, 2, 3] null ");
19+
20+
// First, managed
21+
ValueIterator<JrsValue> it = treeJSON.treeSequenceFrom(INPUT);
22+
_verifyTreeSequence(it);
23+
it.close();
24+
25+
// and parser we create
26+
JsonParser p = treeJSON.createParser(new StringReader(INPUT));
27+
28+
it = treeJSON.treeSequenceFrom(p);
29+
_verifyTreeSequence(it);
30+
it.close();
31+
p.close();
32+
}
33+
34+
private void _verifyTreeSequence(ValueIterator<JrsValue> it) throws Exception
35+
{
36+
assertTrue(it.hasNext());
37+
JrsValue tree = it.nextValue();
38+
assertTrue(tree.isObject());
39+
assertEquals(2, tree.size());
40+
assertEquals(NumberType.INT, tree.path("id").numberType());
41+
assertEquals("foo", tree.path("msg").asText());
42+
43+
assertTrue(it.hasNext());
44+
tree = it.nextValue();
45+
assertTrue(tree.isArray());
46+
assertEquals(3, tree.size());
47+
assertTrue(tree.get(0).isNumber());
48+
assertTrue(tree.get(1).isNumber());
49+
assertEquals(NumberType.INT, tree.get(2).numberType());
50+
51+
assertTrue(it.hasNext());
52+
assertNull(it.nextValue());
53+
54+
assertFalse(it.hasNext());
55+
}
56+
}

0 commit comments

Comments
 (0)