simdjson-java icon indicating copy to clipboard operation
simdjson-java copied to clipboard

Converting JsonValue to String

Open heykirby opened this issue 1 year ago • 14 comments
trafficstars

Simdjson cannot compress structures for example json { "field1": { "field2": "xx" } } I cannot get the compressed value for field1, value is { "field2": "xx" }

heykirby avatar Jan 23 '24 03:01 heykirby

Do I understand correctly that you would like to read the value of field1 as a string? Something like this:

JsonValue json = parser.parse(...);
Iterator<Map.Entry<String, JsonValue>> it = json.objectIterator();
Map.Entry<String, JsonValue> field1 = it.next();
System.out.println(field1.getValue().asString()); // this prints '{ "field2": "xx" }'

piotrrzysko avatar Feb 16 '24 15:02 piotrrzysko

yes, it looks like simdjson has implemented this capability,thanks

heykirby avatar Feb 20 '24 06:02 heykirby

for me field1.getValue().asString() doesn't work if the value is either <array> or <object> gets me something like this instead

Caused by: java.lang.StringIndexOutOfBoundsException: Range [3198, 3198 + 1952541807) out of bounds for length 35651584
         at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
         at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
         at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
         at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
         at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
         at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:118)
         at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:397)
         at java.base/java.lang.String.checkBoundsOffCount(String.java:4849)
         at java.base/java.lang.String.<init>(String.java:522)
         at org.simdjson.JsonValue.getString(JsonValue.java:87)
         at org.simdjson.JsonValue.asString(JsonValue.java:81)

andyglow avatar Feb 21 '24 08:02 andyglow

The snippet I provided in the previous comment was just to verify if I understand the issue correctly. I'm not saying that currently this line works according to the comment:

System.out.println(field1.getValue().asString()); // this prints '{ "field2": "xx" }'

piotrrzysko avatar Feb 21 '24 09:02 piotrrzysko

gotcha. are there plans to make asString handle these scenarios, @piotrrzysko ? in some cases it would be great to be able to reuse original slice of buffer to stringify the value instead of building a new one traversing the value

andyglow avatar Feb 21 '24 20:02 andyglow

I think we can try to add it. However, I'm currently busy working on https://github.com/simdjson/simdjson-java/issues/35, so I can't promise when I'll be able to do that.

piotrrzysko avatar Feb 23 '24 13:02 piotrrzysko

I see. That would be wonderful if you could make it happen at some point. I'll be waiting for it :)

andyglow avatar Mar 05 '24 19:03 andyglow

hello @piotrrzysko. a gentle reminder that this feature still highly anticipated and appreciated :)

andyglow avatar Sep 09 '24 06:09 andyglow

@andyglow refer to #59 ,it works for us,and might work for you too

heykirby avatar Oct 08 '24 08:10 heykirby