google / google/gson

TypeAdapter to read "raw" json value

Open
#1,368 5 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

Since #667 `JsonWriter#jsonValue()` allows emitting a raw json blob when generating json.

I'd like the equivalent for parsing json. In my case, I have large/complex json sub-trees that I would like to avoid parsing to save cpu/allocations. But I still need the value so I can re-create the original json if needed.

Currently I use a typeadapter as so:

```kotlin
class RawJsonAdapter: TypeAdapter() {
override fun write(out: JsonWriter?, value: String?) {
out?.jsonValue(value)
}
override fun read(reader: JsonReader?): String {
return JsonParser().parse(reader).toString()
}
}
```

However the entire subtree has to be first parsed, and then re-serialized to json before I end up with the raw value.

Is there a more performant way to implement my `TypeAdapter#read` here?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.