javax.json.bind.config.PropertyNamingStrategy Java Examples

The following examples show how to use javax.json.bind.config.PropertyNamingStrategy. 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: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenUPPER_CAMEL_CASE_WITH_SPACESStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() {
    /*
        {
          "Alternativetitle": "Fun with JSON-B",
          "Author Name": {
            "First Name": "Alex",
            "Last Name": "Theedom"
          },
          "Title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"Alternativetitle\":\"Fun with JSON-B\",\"Author Name\":{\"First Name\":\"Alex\",\"Last Name\":\"Theedom\"},\"Title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE_WITH_SPACES);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #2
Source File: JsonbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() {
    JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
    Jsonb jsonb = JsonbBuilder.create(config);
    Person person = new Person(1, "Jhon", "[email protected]", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
    String jsonPerson = jsonb.toJson(person);
    // @formatter:off
    String jsonExpected = 
        "{\"email\":\"[email protected]\"," +
         "\"id\":1," +
         "\"person-name\":\"Jhon\"," +
         "\"registered_date\":\"07-09-2019\"," +
         "\"salary\":\"1000.0\"}";
    // @formatter:on
    assertTrue(jsonExpected.equals(jsonPerson));
}
 
Example #3
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenCASE_INSENSITIVEStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() {
    /*
        {
          "alternativetitle": "Fun with JSON-B",
          "authorName": {
            "firstName": "Alex",
            "lastName": "Theedom"
          },
          "title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #4
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenUPPER_CAMEL_CASEStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() {
    /*
        {
          "Alternativetitle": "Fun with JSON-B",
          "AuthorName": {
            "FirstName": "Alex",
            "LastName": "Theedom"
          },
          "Title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"Alternativetitle\":\"Fun with JSON-B\",\"AuthorName\":{\"FirstName\":\"Alex\",\"LastName\":\"Theedom\"},\"Title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #5
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenLOWER_CASE_WITH_UNDERSCORESStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() {
    /*
        {
          "alternativetitle": "Fun with JSON-B",
          "author_name": {
            "first_name": "Alex",
            "last_name": "Theedom"
          },
          "title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"author_name\":{\"first_name\":\"Alex\",\"last_name\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #6
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenLOWER_CASE_WITH_DASHESStrategy_shouldDeserialiseCorrectly() {
    /*
        {
          "alternativetitle": "Fun with JSON-B",
          "author-name": {
            "first-name": "Alex",
            "last-name": "Theedom"
          },
          "title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"author-name\":{\"first-name\":\"Alex\",\"last-name\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES);

    Magazine magazine = JsonbBuilder.create(jsonbConfig).fromJson(expectedJson, Magazine.class);

    assertThat(magazine.getAlternativetitle()).isEqualTo("Fun with JSON-B");
    assertThat(magazine.getAuthorName().getFirstName()).isEqualTo("Alex");
    assertThat(magazine.getAuthorName().getLastName()).isEqualTo("Theedom");
    assertThat(magazine.getTitle()).isEqualTo("Fun with JSON binding");
}
 
Example #7
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenLOWER_CASE_WITH_DASHESStrategy_shouldDelimitPropertyNameWithDashes() {
    /*
        {
          "alternativetitle": "Fun with JSON-B",
          "author-name": {
            "first-name": "Alex",
            "last-name": "Theedom"
          },
          "title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"author-name\":{\"first-name\":\"Alex\",\"last-name\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #8
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@Test
public void givenIDENTITYStrategy_shouldNotChangeAnyPropertyName() {
    /*
        {
          "alternativetitle": "Fun with JSON-B",
          "authorName": {
            "firstName": "Alex",
            "lastName": "Theedom"
          },
          "title": "Fun with JSON binding"
        }
     */

    String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.IDENTITY);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #9
Source File: JsonBindingExample.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
public String customizedMapping() {

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
Example #10
Source File: CustomisePropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenPropertyNameStrategyAndJsonbProperty_JsonbPropertyShouldHavePrecedence() {
    String expectedJson = "{\"name\":\"Alex Theedom\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);

    Person person = new Person();
    person.authorName = "Alex Theedom";
    String json = JsonbBuilder.create(jsonbConfig).toJson(person);

    assertThat(expectedJson).isEqualTo(json);
}
 
Example #11
Source File: JsonbParser.java    From typescript-generator with MIT License 5 votes vote down vote up
private JsonbPropertyExtractor(
        final Class<? extends Annotation> johnzonAny,
        final PropertyNamingStrategy propertyNamingStrategy,
        final PropertyVisibilityStrategy visibilityStrategy,
        final BaseAccessMode delegate) {
    this.johnzonAny = johnzonAny;
    this.naming = propertyNamingStrategy;
    this.visibility = visibilityStrategy;
    this.delegate = delegate;
}
 
Example #12
Source File: JsonbParser.java    From typescript-generator with MIT License 5 votes vote down vote up
public PropertyNamingStrategy create() {
    if (String.class.isInstance(value)) {
        final String val = value.toString();
        switch (val) {
            case PropertyNamingStrategy.IDENTITY:
                return propertyName -> propertyName;
            case PropertyNamingStrategy.LOWER_CASE_WITH_DASHES:
                return new ConfigurableNamingStrategy(Character::toLowerCase, '-');
            case PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES:
                return new ConfigurableNamingStrategy(Character::toLowerCase, '_');
            case PropertyNamingStrategy.UPPER_CAMEL_CASE:
                return camelCaseStrategy();
            case PropertyNamingStrategy.UPPER_CAMEL_CASE_WITH_SPACES:
                final PropertyNamingStrategy camelCase = camelCaseStrategy();
                final PropertyNamingStrategy space = new ConfigurableNamingStrategy(Function.identity(), ' ');
                return propertyName -> camelCase.translateName(space.translateName(propertyName));
            case PropertyNamingStrategy.CASE_INSENSITIVE:
                return propertyName -> propertyName;
            default:
                throw new IllegalArgumentException(val + " unknown as PropertyNamingStrategy");
        }
    }
    if (PropertyNamingStrategy.class.isInstance(value)) {
        return PropertyNamingStrategy.class.cast(value);
    }
    throw new IllegalArgumentException(value + " not supported as PropertyNamingStrategy");
}
 
Example #13
Source File: JSONBConfiguration.java    From tomee with Apache License 2.0 5 votes vote down vote up
public JSONBConfiguration() {
	// jsonbConfig offers a lot of configurations.
	JsonbConfig config = new JsonbConfig().withFormatting(true)
			.withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)
			.withDateFormat("yyyy - MM - dd", Locale.ENGLISH);

	jsonb = JsonbBuilder.create(config);
}
 
Example #14
Source File: JSONBConfiguration.java    From tomee with Apache License 2.0 5 votes vote down vote up
public JSONBConfiguration() {
	// jsonbConfig offers a lot of configurations.
	JsonbConfig config = new JsonbConfig().withFormatting(true)
			.withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)
			.withDateFormat("yyyy - MM - dd", Locale.ENGLISH);

	jsonb = JsonbBuilder.create(config);
}
 
Example #15
Source File: JsonBindingExample.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
public String allCustomizedMapping() {

        PropertyVisibilityStrategy vis = new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return false;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        };

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withPropertyVisibilityStrategy(vis)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true)
                .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
                .withDateFormat("MM/dd/yyyy", Locale.ENGLISH);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
Example #16
Source File: JsonbParser.java    From typescript-generator with MIT License 4 votes vote down vote up
private PropertyNamingStrategy camelCaseStrategy() {
    return propertyName -> Character.toUpperCase(propertyName.charAt(0)) + (propertyName.length() > 1 ? propertyName.substring(1) : "");
}
 
Example #17
Source File: PropertyNamingStrategyTest.java    From Java-EE-8-Sampler with MIT License 3 votes vote down vote up
@Test
public void givenPropertyNamingStrategy_shouldSerialise() {

    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES);

    String json = JsonbBuilder.create(jsonbConfig).toJson(new Book());

    assertThat(json).isEqualTo("{\"author-name\":\"Alex Theedom\",\"book-price\":19.99,\"book-title\":\"Fun with JSON Binding\"}");

}