com.squareup.moshi.Json Java Examples

The following examples show how to use com.squareup.moshi.Json. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoshiClassBuilder.java    From json2java4idea with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected List<FieldSpec> buildFields() {
    return getProperties().entrySet().stream().map(property -> {
        final String name = property.getKey();
        final TypeName type = property.getValue();
        final String fieldName = fieldNamePolicy.convert(name, type);
        return FieldSpec.builder(type, fieldName)
                .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
                .addAnnotation(AnnotationSpec.builder(Json.class)
                        .addMember("name", "$S", name)
                        .build())
                .build();
    }).collect(Collectors.toList());
}
 
Example #2
Source File: MoshiJsonNameMapping.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
@Override
public String getJsonName(Field field) {
    String name = field.getName();
    Json json = field.getAnnotation(Json.class);
    if (json != null) {
        name = json.name();
    }
    return name;
}
 
Example #3
Source File: MoshiClassBuilderTest.java    From json2java4idea with Apache License 2.0 4 votes vote down vote up
@Test
public void buildFields() throws Exception {
    // setup
    underTest.addProperty("foo", TypeName.INT)
            .addProperty("bar", TypeName.OBJECT)
            .addProperty("baz", TypeName.BOOLEAN);

    // exercise
    final List<FieldSpec> actual = underTest.buildFields();

    // verify
    assertThat(actual)
            .hasSize(3)
            .doesNotContainNull();

    assertThat(actual.get(0))
            .hasType(TypeName.INT)
            .hasName("foo")
            .isPrivate()
            .isFinal()
            .hasNoInitializer()
            .hasAnnotation(AnnotationSpec.builder(Json.class)
                    .addMember("name", "$S", "foo")
                    .build());

    assertThat(actual.get(1))
            .hasType(TypeName.OBJECT)
            .hasName("bar")
            .isPrivate()
            .isFinal()
            .hasNoInitializer()
            .hasAnnotation(AnnotationSpec.builder(Json.class)
                    .addMember("name", "$S", "bar")
                    .build());

    assertThat(actual.get(2))
            .hasType(TypeName.BOOLEAN)
            .hasName("baz")
            .isPrivate()
            .isFinal()
            .hasNoInitializer()
            .hasAnnotation(AnnotationSpec.builder(Json.class)
                    .addMember("name", "$S", "baz")
                    .build());
}