Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #148: does not work #161

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
- "README.md"
- "release-notes/*"
permissions:
contents:read
contents: read

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class BeanConstructors

protected Constructor<?> _noArgsCtor;

// @since 2.18
protected Constructor<?> _recordCtor;

protected Constructor<?> _intCtor;
protected Constructor<?> _longCtor;
protected Constructor<?> _stringCtor;
Expand All @@ -30,12 +27,6 @@ public BeanConstructors addNoArgsConstructor(Constructor<?> ctor) {
return this;
}

// @since 2.18
public BeanConstructors addRecordConstructor(Constructor<?> ctor) {
_recordCtor = ctor;
return this;
}

public BeanConstructors addIntConstructor(Constructor<?> ctor) {
_intCtor = ctor;
return this;
Expand All @@ -55,9 +46,6 @@ public void forceAccess() {
if (_noArgsCtor != null) {
_noArgsCtor.setAccessible(true);
}
if (_recordCtor != null) {
_recordCtor.setAccessible(true);
}
if (_intCtor != null) {
_intCtor.setAccessible(true);
}
Expand All @@ -76,14 +64,6 @@ protected Object create() throws Exception {
return _noArgsCtor.newInstance((Object[]) null);
}

// @since 2.18
protected Object createRecord(Object[] components) throws Exception {
if (_recordCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have record constructor to use");
}
return _recordCtor.newInstance(components);
}

protected Object create(String str) throws Exception {
if (_stringCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-String constructor to use");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ private POJODefinition _introspectDefinition(Class<?> beanType,
} else if (argType == Long.class || argType == Long.TYPE) {
constructors.addLongConstructor(ctor);
}
} else if (RecordsHelpers.isRecordConstructor(beanType, ctor, propsByName)) {
constructors.addRecordConstructor(ctor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class BeanReader
*/
protected final BeanConstructors _constructors;

protected final boolean _isRecordType;

/**
* Constructors used for deserialization use case
*
Expand All @@ -58,7 +56,6 @@ public BeanReader(Class<?> type, Map<String, BeanPropertyReader> props,
aliasMapping = Collections.emptyMap();
}
_aliasMapping = aliasMapping;
_isRecordType = RecordsHelpers.isRecordType(type);
}

@Deprecated // since 2.17
Expand Down Expand Up @@ -158,22 +155,6 @@ public Object read(JSONReader r, JsonParser p) throws IOException
return _constructors.create(p.getLongValue());
case START_OBJECT:
{
// [jackson-jr#148] Record deser support (2.18)
if (_isRecordType) {
final List<Object> values = new ArrayList<>();

String propName;
for (; (propName = p.nextFieldName()) != null;) {
BeanPropertyReader prop = findProperty(propName);
if (prop == null) {
handleUnknown(r, p, propName);
continue;
}
values.add(prop.getReader().readNext(r, p));
}
return _constructors.createRecord(values.toArray());
}
// If not Record, need to use default (no-args) Constructors
Object bean = _constructors.create();
String propName;
final Object[] valueBuf = r._setterBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,25 +473,16 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
setter = null;
}
}
if (RecordsHelpers.isRecordType(raw)) {
try {
field = raw.getDeclaredField(rawProp.name);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("Cannot access field '" + rawProp.name
+ "' of record class `" + raw.getName() + "`", e);
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
} else {
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
}

Expand Down
20 changes: 3 additions & 17 deletions jr-record-test/src/test-jdk17/java/jr/Java17RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@ public class Java17RecordTest extends TestCase
public record Cow(String message, Map<String, String> object) {
}

// [jackson-jr#94]
// [jackson-jr#94]: Record serialization
public void testJava14RecordSerialization() throws Exception {
JSON json = JSON.std;
var expectedDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow input = new Cow("MOO", Map.of("Foo", "Bar"));

assertEquals(expectedDoc, json.asString(input));
}

// [jackson-jr#148]
public void testJava14RecordDeserialization() throws Exception {
JSON json = JSON.std;
String inputDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";

Cow expected = new Cow("MOO", Map.of("Foo", "Bar"));

Cow actual = json.beanFrom(Cow.class, inputDoc);
assertEquals(expected, actual);
assertEquals("{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}",
JSON.std.asString(new Cow("MOO", Map.of("Foo", "Bar"))));
}
}
5 changes: 0 additions & 5 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ Julian Honnen (@jhonnen)
* Contributed fix for #90: `USE_BIG_DECIMAL_FOR_FLOATS` feature not working
when using `JSON.treeFrom()`
(2.17.1)

Tomasz Gawęda (@TomaszGaweda)

* Contributed #148: Add support for Java Record deserialization
(2.18.0)
3 changes: 1 addition & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Modules:

2.18.0 (not yet released)

#148: Add support for Java Record deserialization
(contributed by Tomasz G)
-

2.17.1 (04-May-2024)

Expand Down