11package org .simdjson ;
22
3+ import java .util .Arrays ;
34import java .util .Iterator ;
45import java .util .Map ;
56
@@ -60,7 +61,7 @@ public Iterator<JsonValue> arrayIterator() {
6061 return new ArrayIterator (tapeIdx );
6162 }
6263
63- public Iterator <Map .Entry <CharSequence , JsonValue >> objectIterator () {
64+ public Iterator <Map .Entry <String , JsonValue >> objectIterator () {
6465 return new ObjectIterator (tapeIdx );
6566 }
6667
@@ -76,32 +77,34 @@ public boolean asBoolean() {
7677 return tape .getType (tapeIdx ) == TRUE_VALUE ;
7778 }
7879
79- public CharSequence asCharSequence () {
80- return asCharSequence (tapeIdx );
80+ public String asString () {
81+ return getString (tapeIdx );
8182 }
8283
83- private CharSequence asCharSequence (int idx ) {
84- int stringBufferIdx = (int ) tape .getValue (idx );
84+ private String getString (int tapeIdx ) {
85+ int stringBufferIdx = (int ) tape .getValue (tapeIdx );
8586 int len = IntegerUtils .toInt (stringBuffer , stringBufferIdx );
86- return new StringView ( stringBufferIdx + Integer .BYTES , len );
87+ return new String ( stringBuffer , stringBufferIdx + Integer .BYTES , len , UTF_8 );
8788 }
8889
8990 public JsonValue get (String name ) {
90- Iterator <Map .Entry <CharSequence , JsonValue >> it = objectIterator ();
91- while (it .hasNext ()) {
92- Map .Entry <CharSequence , JsonValue > entry = it .next ();
93- CharSequence key = entry .getKey ();
94- if (CharSequence .compare (key , name ) == 0 ) {
95- return entry .getValue ();
91+ byte [] bytes = name .getBytes (UTF_8 );
92+ int idx = tapeIdx + 1 ;
93+ int endIdx = tape .getMatchingBraceIndex (tapeIdx ) - 1 ;
94+ while (idx < endIdx ) {
95+ int stringBufferIdx = (int ) tape .getValue (idx );
96+ int len = IntegerUtils .toInt (stringBuffer , stringBufferIdx );
97+ int valIdx = tape .computeNextIndex (idx );
98+ idx = tape .computeNextIndex (valIdx );
99+ int stringBufferFromIdx = stringBufferIdx + Integer .BYTES ;
100+ int stringBufferToIdx = stringBufferFromIdx + len ;
101+ if (Arrays .compare (bytes , 0 , bytes .length , stringBuffer , stringBufferFromIdx , stringBufferToIdx ) == 0 ) {
102+ return new JsonValue (tape , valIdx , stringBuffer , buffer );
96103 }
97104 }
98105 return null ;
99106 }
100107
101- public String asString () {
102- return asCharSequence ().toString ();
103- }
104-
105108 public int getSize () {
106109 return tape .getScopeCount (tapeIdx );
107110 }
@@ -119,7 +122,7 @@ public String toString() {
119122 return String .valueOf (asBoolean ());
120123 }
121124 case STRING -> {
122- return asCharSequence (). toString ();
125+ return asString ();
123126 }
124127 case NULL_VALUE -> {
125128 return "null" ;
@@ -160,7 +163,7 @@ public JsonValue next() {
160163 }
161164 }
162165
163- private class ObjectIterator implements Iterator <Map .Entry <CharSequence , JsonValue >> {
166+ private class ObjectIterator implements Iterator <Map .Entry <String , JsonValue >> {
164167
165168 private final int endIdx ;
166169
@@ -177,27 +180,27 @@ public boolean hasNext() {
177180 }
178181
179182 @ Override
180- public Map .Entry <CharSequence , JsonValue > next () {
181- CharSequence key = asCharSequence (idx );
183+ public Map .Entry <String , JsonValue > next () {
184+ String key = getString (idx );
182185 idx = tape .computeNextIndex (idx );
183186 JsonValue value = new JsonValue (tape , idx , stringBuffer , buffer );
184187 idx = tape .computeNextIndex (idx );
185188 return new ObjectField (key , value );
186189 }
187190 }
188191
189- private static class ObjectField implements Map .Entry <CharSequence , JsonValue > {
192+ private static class ObjectField implements Map .Entry <String , JsonValue > {
190193
191- private final CharSequence key ;
194+ private final String key ;
192195 private final JsonValue value ;
193196
194- ObjectField (CharSequence key , JsonValue value ) {
197+ ObjectField (String key , JsonValue value ) {
195198 this .key = key ;
196199 this .value = value ;
197200 }
198201
199202 @ Override
200- public CharSequence getKey () {
203+ public String getKey () {
201204 return key ;
202205 }
203206
@@ -211,35 +214,4 @@ public JsonValue setValue(JsonValue value) {
211214 throw new UnsupportedOperationException ("Object fields are immutable" );
212215 }
213216 }
214-
215- private class StringView implements CharSequence {
216-
217- private final int startIdx ;
218- private final int len ;
219-
220- StringView (int startIdx , int len ) {
221- this .startIdx = startIdx ;
222- this .len = len ;
223- }
224-
225- @ Override
226- public int length () {
227- return len ;
228- }
229-
230- @ Override
231- public char charAt (int index ) {
232- return (char ) stringBuffer [startIdx + index ];
233- }
234-
235- @ Override
236- public CharSequence subSequence (int start , int end ) {
237- return new StringView (startIdx + start , startIdx + end );
238- }
239-
240- @ Override
241- public String toString () {
242- return new String (stringBuffer , startIdx , len , UTF_8 );
243- }
244- }
245217}
0 commit comments