Skip to content

Commit

Permalink
Json arrays support
Browse files Browse the repository at this point in the history
  • Loading branch information
nehaev committed Dec 15, 2024
1 parent 5cc37c9 commit 955733b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,28 @@ public void writeEndObject() {
raw.writeByte(OBJECT_END);
}

public void writeBeginArray() {
raw.writeByte(ARRAY_START);
}

public void writeEndArray() {
raw.writeByte(ARRAY_END);
}

public void writeFieldSeparator() {
raw.writeByte(COMMA);
}

public void writeArraySeparator() {
raw.writeByte(COMMA);
}

public void writeObjectField(String fieldName, Object value) {
serializeFieldName(fieldName);
writeObjectValue(value);
}

public void writeObjectValue(Object value) {
// Object is a reference type, first check the value for null
if (value == null) {
raw.writeNull();
Expand All @@ -40,11 +55,13 @@ public void writeObjectField(String fieldName, Object value) {
if (value instanceof String)
raw.writeString((String) value);
else if (value instanceof Integer)
NumberConverter.serialize(((Integer) value).longValue(), raw);
writeNumericValue(((Integer) value).longValue());
else if (value instanceof Long)
NumberConverter.serialize((long) value, raw);
writeNumericValue((long) value);
else if (value instanceof Boolean)
raw.writeBoolean((boolean) value);
else if (value instanceof Iterable)
serializeIterable((Iterable<?>) value);
else if (value instanceof RawJsonString)
raw.writeRawAscii(((RawJsonString) value).value);
else
Expand All @@ -53,15 +70,24 @@ else if (value instanceof RawJsonString)

public void writeStringField(String fieldName, String value) {
serializeFieldName(fieldName);
writeStringValue(value);
}

public void writeStringValue(String value) {
// String is a reference type, first check the value for null
if (value == null)
if (value == null) {
raw.writeNull();
else
} else {
raw.writeString(value);
}
}

public void writeNumericField(String fieldName, long value) {
serializeFieldName(fieldName);
writeNumericValue(value);
}

public void writeNumericValue(long value) {
NumberConverter.serialize(value, raw);
}

Expand All @@ -70,6 +96,17 @@ private void serializeFieldName(String fieldName) {
raw.writeByte(SEMI);
}

private void serializeIterable(Iterable<?> iterable) {
writeBeginArray();
var it = iterable.iterator();
while (it.hasNext()) {
writeObjectValue(it.next());
if (it.hasNext())
writeArraySeparator();
}
writeEndArray();
}

public String toString() {
return raw.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Set;

import org.junit.Test;

public class JsonEventWriterTest {
Expand Down Expand Up @@ -71,4 +74,34 @@ public void testWriteRawJsonStringObject() {

assertEquals("{\"raw\":{\"arr\":[1,2,3],\"val\":null}}", writer.toString());
}

@Test
public void testWriteListOfStrings() {
var writer = new JsonEventWriter(10);
writer.writeBeginObject();
writer.writeObjectField("it", List.of("one", "two", "three"));
writer.writeEndObject();

assertEquals("{\"it\":[\"one\",\"two\",\"three\"]}", writer.toString());
}

@Test
public void testWriteSetOfInts() {
var writer = new JsonEventWriter(10);
writer.writeBeginObject();
writer.writeObjectField("it", Set.of(1, 2, 3));
writer.writeEndObject();

assertEquals("{\"it\":[1,2,3]}", writer.toString());
}

@Test
public void testWriteEmptyList() {
var writer = new JsonEventWriter(10);
writer.writeBeginObject();
writer.writeObjectField("it", List.of());
writer.writeEndObject();

assertEquals("{\"it\":[]}", writer.toString());
}
}

0 comments on commit 955733b

Please sign in to comment.