-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2c538d
commit 2aa7a34
Showing
15 changed files
with
213 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,4 @@ | |
* Dimitrij Drus | ||
* Arno Puder | ||
* SpaceBison | ||
* Tobias Preuss |
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
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
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
51 changes: 51 additions & 0 deletions
51
jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Moshi1Annotator.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,51 @@ | ||
/** | ||
* Copyright © 2016 Tobias Preuss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jsonschema2pojo; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.squareup.moshi.Json; | ||
import com.sun.codemodel.JDefinedClass; | ||
import com.sun.codemodel.JEnumConstant; | ||
import com.sun.codemodel.JFieldVar; | ||
|
||
/** | ||
* Annotates generated Java types using Moshi. The annotations used here are most | ||
* useful when the JSON fields have characters (like underscores) that are | ||
* poorly suited for beans. By using the {@link Json} annotation, we | ||
* are able to preserve the original format. | ||
* | ||
* @see <a | ||
* href="https://github.com/square/moshi#custom-field-names-with-json">https://github.com/square/moshi#custom-field-names-with-json</a> | ||
*/ | ||
public class Moshi1Annotator extends AbstractAnnotator { | ||
|
||
@Override | ||
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) { | ||
field.annotate(Json.class).param("name", propertyName); | ||
} | ||
|
||
@Override | ||
public void enumConstant(JEnumConstant constant, String value) { | ||
constant.annotate(Json.class).param("name", value); | ||
} | ||
|
||
@Override | ||
public boolean isAdditionalPropertiesSupported() { | ||
return false; | ||
} | ||
|
||
} |
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
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
125 changes: 125 additions & 0 deletions
125
...pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/config/Moshi1IT.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,125 @@ | ||
/** | ||
* Copyright © 2016 Tobias Preuss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jsonschema2pojo.integration.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.google.gson.Gson; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Moshi; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config; | ||
import static org.jsonschema2pojo.integration.util.FileSearchMatcher.containsText; | ||
import static org.jsonschema2pojo.integration.util.JsonAssert.assertEqualsJson; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class Moshi1IT { | ||
|
||
@Rule | ||
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule(); | ||
|
||
private Moshi moshi; | ||
|
||
@Before | ||
public void setUp() { | ||
moshi = new Moshi.Builder().build(); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
public void annotationStyleMoshi1ProducesMoshi1Annotations() throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException { | ||
|
||
Class generatedType = schemaRule.generateAndCompile("/json/examples/torrent.json", "com.example", | ||
config("annotationStyle", "moshi1", | ||
"propertyWordDelimiters", "_", | ||
"sourceType", "json")) | ||
.loadClass("com.example.Torrent"); | ||
|
||
assertThat(schemaRule.getGenerateDir(), not(containsText("org.codehaus.jackson"))); | ||
assertThat(schemaRule.getGenerateDir(), not(containsText("com.fasterxml.jackson"))); | ||
assertThat(schemaRule.getGenerateDir(), not(containsText("com.google.gson"))); | ||
assertThat(schemaRule.getGenerateDir(), not(containsText("@SerializedName"))); | ||
assertThat(schemaRule.getGenerateDir(), containsText("com.squareup.moshi")); | ||
assertThat(schemaRule.getGenerateDir(), containsText("@Json")); | ||
|
||
Method getter = generatedType.getMethod("getBuild"); | ||
|
||
assertThat(generatedType.getAnnotation(JsonPropertyOrder.class), is(nullValue())); | ||
assertThat(generatedType.getAnnotation(JsonInclude.class), is(nullValue())); | ||
assertThat(getter.getAnnotation(JsonProperty.class), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void annotationStyleMoshi1MakesTypesThatWorkWithMoshi1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException, IOException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/json/examples/", "com.example", | ||
config("annotationStyle", "moshi1", | ||
"propertyWordDelimiters", "_", | ||
"sourceType", "json", | ||
"useLongIntegers", true)); | ||
|
||
assertJsonRoundTrip(resultsClassLoader, "com.example.Torrent", "/json/examples/torrent.json"); | ||
assertJsonRoundTrip(resultsClassLoader, "com.example.GetUserData", "/json/examples/GetUserData.json"); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
@Test | ||
public void enumValuesAreSerializedCorrectly() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/typeWithEnumProperty.json", "com.example", | ||
config("annotationStyle", "moshi1", | ||
"propertyWordDelimiters", "_")); | ||
|
||
Class generatedType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty"); | ||
Class enumType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty"); | ||
Object instance = generatedType.newInstance(); | ||
|
||
Method setter = generatedType.getMethod("setEnumProperty", enumType); | ||
setter.invoke(instance, enumType.getEnumConstants()[3]); | ||
|
||
JsonAdapter jsonAdapter = moshi.adapter(generatedType); | ||
String json = jsonAdapter.toJson(instance); | ||
|
||
Map<String, String> jsonAsMap = new Gson().fromJson(json, Map.class); | ||
assertThat(jsonAsMap.get("enum_Property"), is("4 ! 1")); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private void assertJsonRoundTrip(ClassLoader resultsClassLoader, String className, String jsonResource) throws ClassNotFoundException, IOException { | ||
Class generatedType = resultsClassLoader.loadClass(className); | ||
|
||
String expectedJson = IOUtils.toString(getClass().getResource(jsonResource)); | ||
JsonAdapter<Object> jsonAdapter = moshi.adapter(generatedType); | ||
Object javaInstance = jsonAdapter.fromJson(expectedJson); | ||
String actualJson = jsonAdapter.toJson(javaInstance); | ||
|
||
assertEqualsJson(expectedJson, actualJson); | ||
} | ||
|
||
} |
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