Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/JSONWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.openhft.chronicle.core.io.ClosedIllegalStateException;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.io.InvalidMarshallableException;
import net.openhft.chronicle.core.io.ValidatableUtil;
import net.openhft.chronicle.core.pool.ClassLookup;
import net.openhft.chronicle.core.threads.ThreadLocalHelper;
import net.openhft.chronicle.core.util.ClassNotFoundRuntimeException;
Expand All @@ -41,6 +42,7 @@
import java.util.function.Supplier;

import static net.openhft.chronicle.bytes.NativeBytes.nativeBytes;
import static net.openhft.chronicle.wire.SerializationStrategies.MARSHALLABLE;

/**
* Represents the JSON wire format.
Expand Down Expand Up @@ -1167,6 +1169,15 @@ public <E> E object(@Nullable E using, @Nullable Class<? extends E> clazz, boole
return useTypes ? parseType(using, clazz, bestEffort) : super.object(using, clazz, bestEffort);
}

@Override
public <E> @Nullable E object(@Nullable E using, @Nullable Class<? extends E> clazz) throws InvalidMarshallableException {
if (useTypes && hasTypeDefinition()) {
return ValidatableUtil.validate((E) parseType());
}

return super.object(using, clazz);
}

@Override
public Class<?> typePrefix() {
return super.typePrefix();
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/net/openhft/chronicle/wire/JSONWireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,30 @@ public void testQuotedFieldsEmptySequence() {
assertEquals("{\"field1\":1234,\"field2\":456,\"field3\":[ ],\"field4\":[\"abc\",\"xyz\" ]}", JSON.asString(f));
}

@Test
public void testNestedListObjectSequence() {
// Create a JSON string with different field types
final Bytes<byte[]> data = Bytes.allocateElasticOnHeap();
data.append("{\n" +
" \"field1\": 1234,\n" +
" \"field2\": 456,\n" +
" \"field4\": [ ],\n" +
" \"field3\": [ { \"@net.openhft.chronicle.wire.JSONWireTest$Item\": {\n" +
" \"name\": \"ones\",\n" +
" \"number1\": 1," +
" \"number2\": 2\n" +
" } } ]\n" +
"}");

final JSONWire wire = new JSONWire(data); // Create a JSONWire object from the data
wire.useTypes(true);
final TwoNestedLists f = new TwoNestedLists(); // Instantiate a new SimpleTwoLists object
wire.getValueIn().object(f, TwoNestedLists.class); // Read the content of the wire into the object

// Assert that the resulting JSON string from the object matches the expected format
assertEquals("{\"field1\":1234,\"field2\":456,\"field3\":[ {\"name\":\"ones\",\"number1\":1,\"number2\":2.0} ],\"field4\":[ ]}", JSON.asString(f));
}

// A static class to demonstrate a holder of maps with different types of numeric keys and string values
static class MapWithIntegerKeysHolder extends SelfDescribingMarshallable {
Map<Integer, String> intMap = new LinkedHashMap<>(); // A map with integer keys
Expand Down Expand Up @@ -640,6 +664,30 @@ private static void readList(SimpleTwoLists record, List<String> data, ValueIn r
}
}

// Class to represent an entity with two fields and two lists of nested objects
private static final class TwoNestedLists implements Marshallable {
int field1; // Integer field 1
int field2; // Integer field 2
final List<Item> field3 = new ArrayList<>(); // List of items for field3
final List<Item> field4 = new ArrayList<>(); // List of items for field4

// Method to read marshallable data from the wire input
@Override
public void readMarshallable(@NotNull final WireIn wire) throws IORuntimeException {
wire.read(() -> "field1").int32(this, (self, n) -> self.field1 = n); // Reading integer value for field1
wire.read(() -> "field2").int32(this, (self, n) -> self.field2 = n); // Reading integer value for field2
wire.read(() -> "field3").sequence(field3, new ArrayList<>(), () -> new Item()); // Reading sequence for field3
wire.read(() -> "field4").sequence(field4, new ArrayList<>(), () -> new Item()); // Reading sequence for field4
}

// Helper method to read and add string values from the reader to a list
private static void readList(SimpleTwoLists record, List<String> data, ValueIn reader) {
while (reader.hasNextSequenceItem()) { // Looping through sequence items
data.add(reader.text()); // Adding each text item to the list
}
}
}

@Test
public void typeLiteral1() {
String expected = "{\"@net.openhft.chronicle.wire.JSONWireTest$DtoWithClassReference\":{\"implClass\":{\"@type\":\"net.openhft.chronicle.wire.JSONWireTest\"},\"bool\":false}}";
Expand Down