Skip to content

Commit

Permalink
bosk-jackson fixes (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle authored Jul 3, 2023
2 parents 9fb8bbe + a896f1a commit 9a86a9d
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 445 deletions.
2 changes: 1 addition & 1 deletion bosk-jackson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

dependencies {
api 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
api 'com.fasterxml.jackson.core:jackson-databind:2.15.1'
api project(":bosk-core")

testImplementation project(":lib-testing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public <T> SerDes<T> compiled(JavaType nodeType, Bosk<?> bosk, FieldModerator mo
ClassBuilder<Codec> cb = new ClassBuilder<>("BOSK_JACKSON_" + nodeClass.getSimpleName(), JacksonCodecRuntime.class, nodeClass.getClassLoader(), here());
cb.beginClass();

generate_writeFields(nodeClass, parameters, cb);
generate_writeFields(nodeType, parameters, cb);
generate_instantiateFrom(constructor, parameters, cb);

Codec codec = cb.buildInstance();

// Return a CodecWrapper for the codec
LinkedHashMap<String, Parameter> parametersByName = new LinkedHashMap<>();
parameters.forEach(p -> parametersByName.put(p.getName(), p));
return new CodecWrapper<>(codec, bosk, nodeClass, parametersByName, moderator);
return new CodecWrapper<>(codec, bosk, nodeType, parametersByName, moderator);
} finally {
Type removed = compilationsInProgress.get().removeLast();
assert removed.equals(nodeType);
Expand Down Expand Up @@ -119,7 +119,9 @@ interface Codec {
/**
* Generates the body of the {@link Codec#writeFields} method.
*/
private void generate_writeFields(Class<?> nodeClass, List<Parameter> parameters, ClassBuilder<Codec> cb) {
private void generate_writeFields(Type nodeType, List<Parameter> parameters, ClassBuilder<Codec> cb) {
JavaType nodeJavaType = TypeFactory.defaultInstance().constructType(nodeType);
Class<?> nodeClass = nodeJavaType.getRawClass();
cb.beginMethod(CODEC_WRITE_FIELDS);
// Incoming arguments
final LocalVariable node = cb.parameter(1);
Expand All @@ -141,7 +143,7 @@ private void generate_writeFields(Class<?> nodeClass, List<Parameter> parameters
// building the plan. The plan should be straightforward and "obviously
// correct". The execution of the plan should contain the sophistication.
FieldWritePlan plan;
JavaType parameterType = TypeFactory.defaultInstance().constructType(parameter.getParameterizedType());
JavaType parameterType = TypeFactory.defaultInstance().resolveMemberType(parameter.getParameterizedType(), nodeJavaType.getBindings());
// TODO: Is the static optimization possible??
// if (compilationsInProgress.get().contains(parameterType)) {
// // Avoid infinite recursion - look up this field's adapter dynamically
Expand Down Expand Up @@ -378,7 +380,7 @@ public void generateFieldWrite(String name, ClassBuilder<Codec> cb, LocalVariabl
private class CodecWrapper<T> implements SerDes<T> {
Codec codec;
Bosk<?> bosk;
Class<T> nodeClass;
JavaType nodeJavaType;
LinkedHashMap<String, Parameter> parametersByName;
FieldModerator moderator;

Expand All @@ -402,9 +404,9 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
// Performance-critical. Pre-compute as much as possible outside this method.
// Note: the reading side can't be as efficient as the writing side
// because we need to tolerate the fields arriving in arbitrary order.
Map<String, Object> valueMap = jacksonPlugin.gatherParameterValuesByName(nodeClass, parametersByName, moderator, p, ctxt);
Map<String, Object> valueMap = jacksonPlugin.gatherParameterValuesByName(nodeJavaType, parametersByName, moderator, p, ctxt);

List<Object> parameterValues = jacksonPlugin.parameterValueList(nodeClass, valueMap, parametersByName, bosk);
List<Object> parameterValues = jacksonPlugin.parameterValueList(nodeJavaType.getRawClass(), valueMap, parametersByName, bosk);

@SuppressWarnings("unchecked")
T result = (T)codec.instantiateFrom(parameterValues);
Expand Down
Loading

0 comments on commit 9a86a9d

Please sign in to comment.