Skip to content

Commit

Permalink
Fix writer for immutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Aug 30, 2024
1 parent 159993f commit 43bd3e4
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/blue/endless/jankson/api/Jankson.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static <T> T readJson(Reader r, JsonReaderOptions opts, Class<T> clazz) t
}

public static void writeJson(Object obj, Writer writer) throws SyntaxError, IOException {
writeJson(obj, new ObjectReaderFactory(), writer, JsonWriterOptions.STRICT);
writeJson(obj, new ObjectReaderFactory(), writer, JsonWriterOptions.DEFAULTS);
}

public static void writeJson(Object obj, ObjectReaderFactory factory, Writer writer, JsonWriterOptions options) throws SyntaxError, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static StructuredDataFunction<?> getObjectWriter(Type type, StructuredDat
}

if (Map.class.isAssignableFrom(targetClass)) {
if (subject instanceof Map map) {
if (subject instanceof Map) {
// Map<Object, Object> is incorrect, but we cannot construct MapFunction<?, ?>
return new MapFunction<Object, Object>(type, (Map<Object, Object>) subject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;

import blue.endless.jankson.api.SyntaxError;
import blue.endless.jankson.impl.io.SingleStructuredDataReader;

public interface StructuredDataReader {

Expand All @@ -47,4 +48,12 @@ public default void transferTo(StructuredDataWriter writer) throws SyntaxError,
writer.write(d);
}
}

public static StructuredDataReader of(StructuredData data) {
return new SingleStructuredDataReader(data);
}

public static StructuredDataReader of(String value) {
return new SingleStructuredDataReader(new StructuredData(StructuredData.Type.PRIMITIVE, value));
}
}
1 change: 0 additions & 1 deletion src/main/java/blue/endless/jankson/api/io/TomlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

public class TomlReader extends AbstractStructuredDataReader {


/**
* Unfortunately, because of the unpredictable order here, we need to crystallize the config down into an object before we can emit anything.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2018-2024 Falkreon (Isaac Ellingson)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package blue.endless.jankson.impl.io;

import java.io.IOException;

import blue.endless.jankson.api.SyntaxError;
import blue.endless.jankson.api.io.StructuredData;
import blue.endless.jankson.api.io.StructuredDataReader;

/**
* StructuredDataReader that will provide a single data record and then complete.
*/
public class SingleStructuredDataReader implements StructuredDataReader {
private boolean hasNext = true;
private final StructuredData value;

public SingleStructuredDataReader(StructuredData value) {
this.value = value;
}

@Override
public StructuredData next() throws SyntaxError, IOException {
hasNext = false;
return value;
}

@Override
public boolean hasNext() {
return hasNext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public T getResult() {
@SuppressWarnings("unchecked")
@Override
protected void process(StructuredData data) throws SyntaxError, IOException {

if (delegate != null) {
delegate.write(data);
checkDelegate();
Expand Down Expand Up @@ -135,6 +136,7 @@ protected void process(StructuredData data) throws SyntaxError, IOException {
delegate = (StructuredDataFunction<Object>) ObjectWriter.getObjectWriter(fieldType, data, null);
if (delegate != null) {
delegate.write(data);
checkDelegate();
} else {
throw new IllegalStateException("Oops");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Object[] arrangeArguments(Executable method, Map<String, Object> a
String serializedName = parameters[i].getName();
SerializedName annotation = parameters[i].getAnnotation(SerializedName.class);
if (annotation != null) serializedName = annotation.value();
if (!arguments.containsKey(serializedName)) throw new InstantiationException("No value supplied for required field \""+serializedName+"\".");
if (!arguments.containsKey(serializedName)) throw new InstantiationException("No value supplied for required field \""+serializedName+"\". ("+arguments+")");
arrangedArguments[i] = arguments.get(serializedName);
}
return arrangedArguments;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/blue/endless/jankson/TestJsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ public void testBetterRead() throws IOException, SyntaxError {

record Release(String id, String releaseTime, String resources, String time, String type, String url) {}
Release[] releases = Jankson.readJson(new StringReader(subject), JsonReaderOptions.UNSPECIFIED, Release[].class);
System.out.println(Arrays.toString(releases));
//System.out.println(Arrays.toString(releases));
}
}
32 changes: 32 additions & 0 deletions src/test/java/blue/endless/jankson/TestObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,36 @@ public void testFull() throws SyntaxError, IOException {

Assertions.assertEquals(expected, actual);
}

public static class ImmutableConfig {

final int x;
final int y;

public ImmutableConfig(@SerializedName("x") int x, @SerializedName("y") int y) {
this.x = x;
this.y = y;
}

public int getX() { return x; }
public int getY() { return y; }

}

@Test
public void testImmutable() throws SyntaxError, IOException {
String subject = """
{ "x": 12, "y": 24 }
""";

StringReader stringReader = new StringReader(subject);
JsonReader reader = new JsonReader(stringReader);
ObjectWriter<ImmutableConfig> writer = new ObjectWriter<>(ImmutableConfig.class);
reader.transferTo(writer);

ImmutableConfig config = writer.toObject();

Assertions.assertEquals(12, config.x);
Assertions.assertEquals(24, config.y);
}
}

0 comments on commit 43bd3e4

Please sign in to comment.