-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Java 17 & Groovy Records (#130)
- Loading branch information
Showing
8 changed files
with
168 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/ReadRecordLikeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.fasterxml.jackson.jr.ob; | ||
|
||
// For [jackson-jr#94]: support for Serializing JDK 17/Groovy records | ||
// (minimal one; full test in separate test package) | ||
// | ||
// @since 2.17 | ||
public class ReadRecordLikeTest extends TestBase | ||
{ | ||
static class RecordLike94 { | ||
int count = 3; | ||
int STATUS = 500; | ||
int foobar; | ||
|
||
// should be discovered: | ||
public int count() { return count; } | ||
// likewise: | ||
public int STATUS() { return STATUS; } | ||
|
||
// should NOT be discovered (takes argument(s)) | ||
public int foobar(int value) { | ||
foobar = value; | ||
return value; | ||
} | ||
|
||
// also not to be discovered | ||
public int mismatched() { return 42; } | ||
} | ||
|
||
public void testRecordLikePOJO() throws Exception | ||
{ | ||
// By default, do not auto-detect "record-style" accessors | ||
assertEquals("{}", JSON.std.asString(new RecordLike94())); | ||
|
||
assertEquals(a2q("{'STATUS':500,'count':3}"), JSON.std.with(JSON.Feature.USE_FIELD_MATCHING_GETTERS) | ||
.asString(new RecordLike94())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import com.fasterxml.jackson.jr.ob.JSON | ||
import org.junit.Assert | ||
import org.junit.Test | ||
|
||
/** | ||
* A minor note on running/debugging this test on local, if you are using intellij, please | ||
* change `<packaging>pom</packaging>` to `<packaging>bundle</packaging>`. this is causing | ||
* some issue with the IDE. | ||
*/ | ||
class GroovyRecordsTest { | ||
|
||
@Test | ||
void testRecord() throws Exception { | ||
/* We need to use this since build (8, ubuntu-20.04), will fail Map.of() was added in Java 9*/ | ||
def map = new HashMap<String, String>() | ||
map.put("foo", "bar") | ||
|
||
def json = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build().asString(new Cow("foo", map)) | ||
def expected = """{"message":"foo","object":{"foo":"bar"}}""" | ||
Assert.assertEquals(expected, json) | ||
} | ||
|
||
@Test | ||
void testRecordEquivalentObjects() throws Exception { | ||
def expected = """{"message":"foo","object":{"foo":"bar"}}""" | ||
|
||
/* We need to use this since build (8, ubuntu-20.04), will fail Map.of() was added in Java 9*/ | ||
def map = new HashMap<String, String>() | ||
map.put("foo", "bar") | ||
|
||
def json = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build().asString(new SimpleGroovyObject("foo", map)) | ||
Assert.assertEquals(expected, json) | ||
|
||
def json2 = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build().asString(new GroovyObjectWithNamedGetters("foo", map)) | ||
Assert.assertEquals(expected, json2) | ||
} | ||
} | ||
|
||
class SimpleGroovyObject { | ||
public final String message | ||
public final Map<String, String> object | ||
|
||
SimpleGroovyObject(String message, Map<String, String> object) { | ||
this.message = message | ||
this.object = object | ||
} | ||
} | ||
|
||
class GroovyObjectWithNamedGetters { | ||
private final String message | ||
private final Map<String, String> object | ||
|
||
GroovyObjectWithNamedGetters(String message, Map<String, String> object) { | ||
this.message = message | ||
this.object = object | ||
} | ||
|
||
String message() { | ||
return message | ||
} | ||
|
||
Map<String, String> object() { | ||
return object | ||
} | ||
} | ||
|
||
record Cow(String message, Map<String, String> object) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import com.fasterxml.jackson.jr.ob.JSON; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* This test is in test module since the JDK version to be tested is higher than other, and hence supports Records. | ||
*/ | ||
public class Java17RecordTest { | ||
|
||
@Test | ||
public void testJava14RecordSupport() throws IOException { | ||
var expectedString = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}"; | ||
var json = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build().asString(new Cow("MOO", Map.of("Foo", "Bar"))); | ||
Assert.assertEquals(expectedString, json); | ||
} | ||
|
||
record Cow(String message, Map<String, String> object) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters