Skip to content

Commit

Permalink
Make default priority for factory methods 0
Browse files Browse the repository at this point in the history
Additional serialization work
  • Loading branch information
rchodava committed Dec 8, 2016
1 parent 5f1b7ca commit 20b51a6
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ public Wiring add(Object... additions) {

/**
* Add a factory method to the Wiring which is invoked to create an instance of the specified class. The factory
* method is added with a default priority of {@link Integer#MIN_VALUE}
* method is added with a default priority of 0.
*
* @param clazz Class for which we want to add a factory.
* @param factory Factory method to invoke for the class specified.
*/
public <T> Wiring addFactory(Class<T> clazz, Func1<Wiring, T> factory) {
return addFactory(Integer.MIN_VALUE, clazz, factory);
return addFactory(0, clazz, factory);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package foundation.stack.datamill.db.impl;

import foundation.stack.datamill.reflection.Member;
import foundation.stack.datamill.serialization.DeserializationStrategy;
import foundation.stack.datamill.serialization.StructuredInput;
import foundation.stack.datamill.values.Value;
import foundation.stack.datamill.db.DatabaseException;
import foundation.stack.datamill.db.Row;
import rx.functions.Action1;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/foundation/stack/datamill/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
*/
public interface Json {
static <T> Deserializer<JsonObject, T> deserializer(DeserializationStrategy<T> strategy) {
return json -> {
T object = null;
return object;
};
return json -> strategy.deserialize(json);
}

static <T> Serializer<T, JsonObject> serializer(SerializationStrategy<T> strategy) {
Expand Down
200 changes: 194 additions & 6 deletions core/src/main/java/foundation/stack/datamill/json/JsonObject.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package foundation.stack.datamill.json;

import foundation.stack.datamill.reflection.Member;
import foundation.stack.datamill.serialization.StructuredOutput;
import foundation.stack.datamill.serialization.*;
import foundation.stack.datamill.values.*;
import foundation.stack.datamill.reflection.impl.TripleArgumentTypeSwitch;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import rx.functions.Action1;

import java.time.LocalDateTime;
import java.util.*;
Expand All @@ -15,7 +16,7 @@
/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class JsonObject implements Json, ReflectableValue, StructuredOutput<JsonObject> {
public class JsonObject implements Json, ReflectableValue, StructuredOutput<JsonObject>, DeepStructuredInput {
private static final TripleArgumentTypeSwitch<JSONObject, String, JsonProperty, Object> propertyAsObjectSwitch =
new TripleArgumentTypeSwitch<JSONObject, String, JsonProperty, Object>() {
@Override
Expand Down Expand Up @@ -168,14 +169,60 @@ public String asString() {
return object.toString();
}

@Override
public DeepStructuredInput forEach(String name, Action1<Value> action) {
Object child = object.opt(name);
if (child instanceof JSONArray) {
JSONArray array = (JSONArray) child;
for (int i = 0; i < array.length(); i++) {
action.call(new JsonElement(array, i));
}
} else {
action.call(new JsonProperty(name));
}

return this;
}

@Override
public <T> DeepStructuredInput forEach(String name, DeserializationStrategy<T> strategy, Action1<T> action) {
JSONObject child = object.optJSONObject(name);
if (child != null) {
T deserialized = strategy.deserialize(new JsonObject(child));
action.call(deserialized);
} else {
JSONArray array = object.optJSONArray(name);
if (array != null) {
for (int i = 0; i < array.length(); i++) {
T deserialized = strategy.deserialize(new JsonObject(array.getJSONObject(i)));
action.call(deserialized);
}
}
}

return this;
}

@Override
public Value get(String property) {
return new JsonProperty(property);
}

@Override
public Value get(Member member) {
return get(member.name());
}

@Override
public <T> T get(String name, DeserializationStrategy<T> strategy) {
JSONObject child = object.optJSONObject(name);
if (child != null) {
return strategy.deserialize(new JsonObject(child));
}

return null;
}

@Override
public boolean isBoolean() {
return false;
Expand Down Expand Up @@ -237,6 +284,41 @@ public <T> JsonObject put(String key, T value) {
return this;
}

@Override
public <T> JsonObject put(String key, T value, SerializationStrategy<T> strategy) {
if (value != null) {
JsonObject serialized = new JsonObject();
serialized = (JsonObject) strategy.serialize(serialized, value);

if (serialized != null) {
object.put(key, serialized.object);
}
}

return this;
}

@Override
public <T> JsonObject put(String key, Iterable<T> values, SerializationStrategy<T> strategy) {
if (values != null) {
JSONArray array = new JSONArray();
for (T value : values) {
JsonObject serialized = new JsonObject();
serialized = (JsonObject) strategy.serialize(serialized, value);

if (serialized != null) {
array.put(serialized.object);
} else {
array.put((Object) null);
}
}

this.object.put(key, array);
}

return this;
}

public JsonObject put(String key, JsonObject object) {
this.object.put(key, object.object);
return this;
Expand Down Expand Up @@ -269,6 +351,112 @@ public String toString() {
return asString();
}

private class JsonElement implements Value {
private final JSONArray array;
private final int index;

private JsonElement(JSONArray array, int index) {
this.array = array;
this.index = index;
}

@Override
public boolean asBoolean() {
return array.getBoolean(index);
}

@Override
public byte asByte() {
return (byte) array.getInt(index);
}

@Override
public byte[] asByteArray() {
try {
JSONArray array = this.array.getJSONArray(index);
if (array != null) {
byte[] bytes = new byte[array.length()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) array.getInt(i);
}

return bytes;
}
} catch (JSONException e) {
String value = asString();
if (value != null) {
return value.getBytes();
}
}

return null;
}

@Override
public char asCharacter() {
try {
return (char) array.getInt(index);
} catch (JSONException e) {
String value = array.getString(index);
if (value.length() == 1) {
return value.charAt(0);
}

throw new JsonException("Property cannot be converted to a character!");
}
}

@Override
public double asDouble() {
return array.getDouble(index);
}

@Override
public float asFloat() {
return (float) array.getDouble(index);
}

@Override
public int asInteger() {
return array.getInt(index);
}
@Override
public LocalDateTime asLocalDateTime() {
String value = array.optString(index);
if (value != null) {
return LocalDateTime.parse(value);
}

return null;
}

@Override
public long asLong() {
return array.getLong(index);
}

@Override
public Object asObject(Class<?> type) {
// return propertyAsObjectSwitch.doSwitch(type, object, name, this);
return null;
}

@Override
public short asShort() {
return (short) array.getInt(index);
}

@Override
public String asString() {
return array.optString(index);
}

@Override
public <T> T map(Function<Value, T> mapper) {
return mapper.apply(this);
}
}

public class JsonProperty implements Value {
private String name;

Expand All @@ -283,7 +471,7 @@ public boolean asBoolean() {

@Override
public byte asByte() {
return (byte) (int) object.getInt(name);
return (byte) object.getInt(name);
}

@Override
Expand Down Expand Up @@ -311,7 +499,7 @@ public byte[] asByteArray() {
@Override
public char asCharacter() {
try {
return (char) (int) object.getInt(name);
return (char) object.getInt(name);
} catch (JSONException e) {
String value = object.getString(name);
if (value.length() == 1) {
Expand All @@ -329,7 +517,7 @@ public double asDouble() {

@Override
public float asFloat() {
return object.getBigDecimal(name).floatValue();
return (float) object.getDouble(name);
}

@Override
Expand Down Expand Up @@ -387,7 +575,7 @@ public String asString() {

@Override
public <T> T map(Function<Value, T> mapper) {
return null;
return mapper.apply(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package foundation.stack.datamill.serialization;

import foundation.stack.datamill.values.Value;
import rx.functions.Action1;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public interface DeepStructuredInput extends StructuredInput {
<T> T get(String name, DeserializationStrategy<T> strategy);

DeepStructuredInput forEach(String name, Action1<Value> action);

<T> DeepStructuredInput forEach(String name, DeserializationStrategy<T> strategy, Action1<T> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
@FunctionalInterface
public interface DeserializationStrategy<T> {
void deserialize(T target, StructuredInput source);
T deserialize(DeepStructuredInput source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
@FunctionalInterface
public interface Deserializer<SerializedType extends StructuredOutput, TargetType>
public interface Deserializer<SerializedType extends DeepStructuredInput, TargetType>
extends Func1<SerializedType, TargetType> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ public class SerializationStrategyBuilder<SourceType> {
Func1<OutlineBuilder, Outline<SourceType>>,
Func3<StructuredOutput<?>, SourceType, Outline<?>, StructuredOutput<?>>>> strategies = new ArrayList<>();

public SerializationStrategyBuilder<SourceType> withOutline(
Func1<OutlineBuilder, Outline<SourceType>> outlineBuilder,
Func3<StructuredOutput<?>, SourceType, Outline<SourceType>, StructuredOutput<?>> strategy) {
strategies.add(new Pair(outlineBuilder, strategy));
outlines.add(null);
return this;
}

public SerializationStrategy<SourceType> build() {
return (target, source) -> {
for (int i = 0; i < strategies.size(); i++) {
Expand All @@ -45,4 +37,12 @@ public SerializationStrategy<SourceType> build() {
return target;
};
}

public SerializationStrategyBuilder<SourceType> withOutline(
Func1<OutlineBuilder, Outline<SourceType>> outlineBuilder,
Func3<StructuredOutput<?>, SourceType, Outline<SourceType>, StructuredOutput<?>> strategy) {
strategies.add(new Pair(outlineBuilder, strategy));
outlines.add(null);
return this;
}
}
Loading

0 comments on commit 20b51a6

Please sign in to comment.