From what you posted it looks similar to this since the outputs are separated with a nextline character. You could make use of it to split the string into an array of serialized json responses.

Then you could individually parse them into json objects.

Answer from humblerookie on Stack Overflow
Top answer
1 of 1
6

Jackson will normally run into problems when serializing complex objects with lots of internal state and/or non-standard getter/setter methods. In general, you should always stick to pure POJOs in the object hierarchy to be serialized.

In this particular case, you've created a wrapper that contains a ByteBuffer. Well, Jackson actually tries to serialize the entire byte buffer object (not just its byte contents), and if you look under the covers its actually trying to write out all these 'properties':

[
    property 'short' (via method java.nio.HeapByteBuffer#getShort), 
    property 'char' (via method java.nio.HeapByteBuffer#getChar),
    property 'int' (via method java.nio.HeapByteBuffer#getInt),
    property 'long' (via method java.nio.HeapByteBuffer#getLong),
    property 'float' (via method java.nio.HeapByteBuffer#getFloat),
    property 'double' (via method java.nio.HeapByteBuffer#getDouble),
    property 'direct' (via method java.nio.HeapByteBuffer#isDirect),
    property 'readOnly' (via method java.nio.HeapByteBuffer#isReadOnly)
]

Its just by dumb luck that the second case works (because the buffer is long enough to get all the above shown methods invoked on it without underflowing). If you want to serialize the buffer as bytes, then either:

  • change the wrapper property to byte[]
  • keep the property as ByteBuffer, but mark it as @JsonIgnoreable and provide an alternate accessor method around it that converts to a byte[]

Example of first:

class Wrapper {
    final byte[] buffer;

    public Wrapper(final ByteBuffer buffer) {
        super();
        this.buffer = buffer != null ? buffer.array() : new byte[0];
    }
}
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 239
Support automatically writing ByteBuffer properties as Base64 · Issue #239 · FasterXML/jackson-databind
June 10, 2013 - It'd be great if Jackson 2.x were able to support POJOs containing ByteBuffer properties by writing them out via: myJsonGenerator.writeBinary(myByteBuffer.array()); And deserialize the resulting Base64 via ByteBuffer.wrap(myJsonParser.ge...
Author: FasterXML
🌐
GitHub
github.com › json-iterator › java › issues › 137
Allow use of ByteBuffer for underlying data · Issue #137 · json-iterator/java
January 2, 2018 - It would be useful to be able to use a ByteBuffer rather than a byte[] to read data from. When using ByteBuffer.allocateDirect there is no underlying byte[] to access and call reset on the JsonIterator object.
Author: json-iterator
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.9 › com › fasterxml › jackson › databind › deser › std › ByteBufferDeserializer.html
ByteBufferDeserializer (jackson-databind 2.9.0 API)
Post-condition is that the parser will point to the last event that is part of deserialized value (or in case deserialization fails, event that was not recognized or usable, which may be the same event as the one it pointed to upon call). Note that this method is never called for JSON null literal, and thus deserializers need (and should) not check for it. ... public ByteBuffer deserialize(JsonParser jp, DeserializationContext ctxt, ByteBuffer intoValue) throws IOException
🌐
Java2s
java2s.com › example › java-api › java › nio › bytebuffer › wrap-1-15.html
Example usage for java.nio ByteBuffer wrap
* * @param jsonObject the json./*from w w w . j av a2 s. c om*/ * @throws JSONException */ public static ByteBuffer getData(JSONObject jsonObject) throws JSONException { return ByteBuffer.wrap(Base64.decode(jsonObject.getString(DATA_FIELD_KEY), Base64.DEFAULT)); } From source file:com.willetinc.hadoop.mapreduce.dynamodb.AttributeValueIOUtils.java ·
🌐
Java67
java67.com › 2015 › 06 › how-to-convert-bytebuffer-to-string-in-java-example.html
How to convert ByteBuffer to String in Java [Example] | Java67
All you need to do is call the ByteBuffer.array() method, it will return you the byte array used by java.nio.ByteBuffer class, later you can easily create String from that byte array.
🌐
Swift Forums
forums.swift.org › using swift
Use JSONDecoder with ByteBuffer - Using Swift - Swift Forums
November 22, 2020 - Hi, I'm currently working on a TCP server using SwiftNIO, and I can't find an easy way to convert a ByteBuffer directly to Data in order to decode it more easily. Here is my inbound handler tasked with parsing the received messages: final class MessageParser: ChannelInboundHandler { typealias InboundIn = ByteBuffer typealias InboundOut = CommsMessage public func channelRead(context: ChannelHandlerContext, data: NIOAny) { do { var buffer = self.unwrapInboundI...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 41867917 › convert-bytes-buffer-to-json-and-unmarshal-in-app-engine
go - Convert *bytes.Buffer to json and Unmarshal in app engine - Stack Overflow
Having trouble decoding JSON URL GET request with golang appengine. I'm new to golang and this is probably a simple fix. Can't convert bytes.Buffer to struct. Am I doing it right? Alternatively I
🌐
DevQA
devqa.io › encode-decode-json-byte-array
How to Encode and Decode JSON Byte Array
November 9, 2019 - Java provides different ways to Base64 encode and decode a byte[]. One of these is DatatypeConverter. ... { "menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } } } ... import javax.xml.bind.DatatypeConverter; public class JsonEncodeDecode { public static void main(String[] args) { String json = "{\"menu\": {\n" + " \"id\": \"file\",\n" + " \"value\": \"File\",\n" + " \"popup\": {\n" + " \"menuitem\": [\n" + " {\"value\": \"New\", \"onclick\": \
🌐
GitHub
gist.github.com › reddikih › 00ed63847f9f4e620e9d4772d69cbee5
java convert bytebuffer to string · GitHub
Save reddikih/00ed63847f9f4e620e9d4772d69cbee5 to your computer and use it in GitHub Desktop. Download ZIP · java convert bytebuffer to string · Raw · convertBytebufferToString.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
MuleSoft
help.mulesoft.com › s › question › 0D52T00004mXWUBSA4 › convert-byte-array-to-json
Convert Byte Array to Json
MuleSoft Help Center provides MuleSoft Forums, resources, training, discussion groups, support and details about the MuleSoft Community.
🌐
Medium
medium.com › @techsuneel99 › how-to-convert-a-buffer-to-json-in-node-js-4fa9ef98160
How to convert a Buffer to JSON in Node.js ? | by Suneel Kumar | Medium
December 31, 2023 - Depending on your specific use case, you might want to structure the JSON differently or include additional information. Adjust the code accordingly based on your requirements. Buffer.values() returns an iterator that yields each byte value in the Buffer. We can use spread syntax to convert it to an array:
🌐
Medium
priyarajtt.medium.com › java-program-to-convert-byte-array-to-json-38548b53e27f
Java Program to Convert Byte Array to JSON | by priya raj | Medium
July 12, 2022 - Arrays — Arrays begin with ‘[‘(open) and end with ‘]’ (end) braces and it is almost similar to the Array data structure. In the JSON array, it will be a collection of different values as JSON strings.
🌐
Swiftpackageregistry
swiftpackageregistry.com › swift-extras › swift-extras-json
swift-extras-json - Swift Package Registry
March 17, 2020 - Add ExtrasJSON to the target you want to use it in. targets: [ .target(name: "MyFancyTarget", dependencies: [ .product(name: "ExtrasJSON", package: "swift-extras-json"), ]) ] Use it as you would use the Foundation encoder and decoder. import ExtrasJSON let bytesArray = try XJSONEncoder().encode(myEncodable) let myDecodable = try XJSONDecoder().decode(MyDecodable.self, from: bytes) For maximal performance create an [UInt8] from your ByteBuffer, even though buffer.readableBytesView would technically work as well.