I have some incoming JSON (the field-order of which is not my choice) that embeds a dependent pair:
{
"data": {...},
"evt": "READY",
...
}
and what type I should read data
into depends on the value of evt
. With just a JsonParser
this is impossible because there's no way to store data
for later so that it can be returned to once evt
is reached.
All of the data I'm parsing (unfortunately) already exists in a ByteBuffer
, so is there a better interface to use than JsonParser
? I don't want to bring in any more dependencies than jackson-core
if it can be helped.
Looks like there is no simple way to achieve this without any additional dependencies.
I suppose, you need to add at least jackson-databind
(and also jackson-annotations
if not added automatically via Maven/Gradle). Then you can use an ObjectMapper
as an ObjectCodec
for the parser and parse the complete JSON either into a TreeNode
structure that can be partically parsed later into the correct type or - if you have objects for all types of data - you maybe can directly parse the complete object with matching data type. If needed, a custom ObjectCodec
could be implemented to first collect the unknown data and then later process it when the type is known, but implementing an ObjectCode
does not seem to be that easy.
Instead of Jackson you could use GSON which can either parse the data into the complete object structure or a generic JSON object tree without any additional dependencies.
If you really cannot add additional dependencies, then you could implement a SAX-XML-Parser-like logic using JsonParser.nextToken
, but I suppose that would require a lot of custom logic.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.