@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); }
@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); }
@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"); }
@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); }
@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); }
@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); }
@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); }
private PropertyNamingStrategy initPropertyNamingStrategy() { final Optional<Object> property = jsonbConfig.getProperty(JsonbConfig.PROPERTY_NAMING_STRATEGY); if (!property.isPresent()) { return new IdentityStrategy(); } Object propertyNamingStrategy = property.get(); if (propertyNamingStrategy instanceof String) { String namingStrategyName = (String) propertyNamingStrategy; final PropertyNamingStrategy foundNamingStrategy = DefaultNamingStrategies.getStrategy(namingStrategyName); if (foundNamingStrategy == null) { throw new JsonbException("No property naming strategy was found for: " + namingStrategyName); } return foundNamingStrategy; } if (!(propertyNamingStrategy instanceof PropertyNamingStrategy)) { throw new JsonbException(Messages.getMessage(MessageKeys.PROPERTY_NAMING_STRATEGY_INVALID)); } return (PropertyNamingStrategy) property.get(); }
/** * Tests clash with property altered by naming strategy. */ @Test public void testConflictingWithUpperCamelStrategy() { ConflictingWithUpperCamelStrategy pojo = new ConflictingWithUpperCamelStrategy(); pojo.setDOI("DOI value"); Jsonb jsonb = JsonbBuilder.create(); String json = jsonb.toJson(pojo); Assert.assertEquals("{\"Doi\":\"DOI value\",\"doi\":\"DOI value\"}", json); jsonb = JsonbBuilder.create(new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)); try { jsonb.toJson(pojo); fail(); } catch (JsonbException e) { if (!e.getMessage().equals("Property DOI clashes with property doi by read or write name in class org.eclipse.yasson.customization.JsonbPropertyTest$ConflictingWithUpperCamelStrategy.")) { throw e; } } }
@Test public void testLowerCase() throws Exception { PropertyNamingStrategy strategy = new LowerCaseWithUnderscoresStrategy(); assertEquals("camel_case_property", strategy.translateName("camelCaseProperty")); assertEquals("camelcase_property", strategy.translateName("CamelcaseProperty")); assertEquals("camel_case_property", strategy.translateName("CamelCaseProperty")); assertEquals("_camel_case_property", strategy.translateName("_camelCaseProperty")); assertEquals("_camel_case_property", strategy.translateName("_CamelCaseProperty")); Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES)); String lowercaseUnderscoresJson = "{\"_starting_with_underscore_property\":\"def\",\"caps_underscore_property\":\"ghi\",\"upper_cased_property\":\"abc\"}"; assertEquals(lowercaseUnderscoresJson, jsonb.toJson(pojo)); NamingPojo result = jsonb.fromJson(lowercaseUnderscoresJson, NamingPojo.class); assertResult(result); }
public static JsonbConfig jsonbConfig() { return new JsonbConfig() // Property visibility .withNullValues(true) .withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() { @Override public boolean isVisible(Field field) { return false; } @Override public boolean isVisible(Method method) { return false; } }) // Property naming and order .withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE) .withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE) // Customised de/serializers .withAdapters(new ClassAdapter()) .withDeserializers(new CustomDeserializer()) .withSerializers(new CustomSerializer()) // Formats, locals, encoding, binary data .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL) .withDateFormat("MM/dd/yyyy", Locale.ENGLISH) .withLocale(Locale.CANADA) .withEncoding("UTF-8") .withStrictIJSON(true) .withFormatting(true); }
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); }
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); }
@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); }
@Test public void testLowerDashes() throws Exception { PropertyNamingStrategy strategy = new LowerCaseWithDashesStrategy(); assertEquals("camel-case-property", strategy.translateName("camelCaseProperty")); assertEquals("camelcase-property", strategy.translateName("CamelcaseProperty")); assertEquals("camel-case-property", strategy.translateName("CamelCaseProperty")); assertEquals("-camel-case-property", strategy.translateName("-camelCaseProperty")); assertEquals("-camel-case-property", strategy.translateName("-CamelCaseProperty")); Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)); String lowercaseDashesJson = "{\"_starting-with-underscore-property\":\"def\",\"caps_underscore_property\":\"ghi\",\"upper-cased-property\":\"abc\"}"; assertEquals(lowercaseDashesJson, jsonb.toJson(pojo)); NamingPojo result = jsonb.fromJson(lowercaseDashesJson, NamingPojo.class); assertResult(result); }
@Test public void testUpperCase() { PropertyNamingStrategy upperCaseStrategy = new UpperCamelCaseStrategy(); assertEquals("UpperCamelCase", upperCaseStrategy.translateName("upperCamelCase")); assertEquals("UpperCamelCase", upperCaseStrategy.translateName("UpperCamelCase")); Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)); String upperCased = "{\"CAPS_UNDERSCORE_PROPERTY\":\"ghi\",\"UpperCasedProperty\":\"abc\",\"_startingWithUnderscoreProperty\":\"def\"}"; assertEquals(upperCased, jsonb.toJson(pojo)); NamingPojo result = jsonb.fromJson(upperCased, NamingPojo.class); assertResult(result); }
@Test public void testUpperCaseWithSpaces() { PropertyNamingStrategy upperCaseWithSpacesStrategy = new UpperCamelCaseWithSpacesStrategy(); assertEquals("Upper Camel Case", upperCaseWithSpacesStrategy.translateName("upperCamelCase")); assertEquals("Upper Camel Case", upperCaseWithSpacesStrategy.translateName("UpperCamelCase")); Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE_WITH_SPACES)); String upperCased = "{\"CAPS_UNDERSCORE_PROPERTY\":\"ghi\",\"Upper Cased Property\":\"abc\",\"_starting With Underscore Property\":\"def\"}"; assertEquals(upperCased, jsonb.toJson(pojo)); NamingPojo result = jsonb.fromJson(upperCased, NamingPojo.class); assertResult(result); }
@Test public void testCaseInsensitive() { Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE)); String upperCased = "{\"CAPS_UNDERSCORE_PROPERTY\":\"ghi\",\"_startingWithUnderscoreProperty\":\"def\",\"upperCasedProperty\":\"abc\"}"; assertEquals(upperCased, jsonb.toJson(pojo)); NamingPojo result = jsonb.fromJson("{\"caPS_unDERscore_prOPERty\":\"ghi\",\"_startingwithUndERSCorePrOPERTy\":\"def\",\"upPERCASedProPerty\":\"abc\"}", NamingPojo.class); assertResult(result); }
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"); }
public JsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy, final String orderValue, final PropertyVisibilityStrategy visibilityStrategy, final boolean caseSensitive, final Map<AdapterKey, Adapter<?, ?>> defaultConverters, final JohnzonAdapterFactory factory, final Supplier<JsonParserFactory> parserFactory, final AccessMode delegate) { this.naming = propertyNamingStrategy; this.order = orderValue; this.visibility = visibilityStrategy; this.caseSensitive = caseSensitive; this.delegate = delegate; this.defaultConverters = defaultConverters; this.factory = factory; this.parserFactory = parserFactory; }
/** * Test that @JsonbPropertyOrder takes java names and pushes at the end not mentionned properties. */ @Test public void run() { final Person p = new Person(); p.setPersonAge(12); p.setPersonName("David"); p.setPersonGender("Male"); assertEquals( "{\"person_gender\":\"Male\",\"person_name\":\"David\",\"person_age\":12}", JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES)) .toJson(p)); }
@DataPoints() public static String[][] points() { return new String[][] { new String[] { PropertyNamingStrategy.IDENTITY, "a", "a" }, new String[] { PropertyNamingStrategy.IDENTITY, "aBEOCBDJ4397dkabqWLCd", "aBEOCBDJ4397dkabqWLCd" }, new String[] { PropertyNamingStrategy.CASE_INSENSITIVE, "aBEOCBDJ4397dkabqWLCd", "aBEOCBDJ4397dkabqWLCd" }, // not really testable there new String[] { PropertyNamingStrategy.LOWER_CASE_WITH_DASHES, "lower-dash", "lower-dash" }, new String[] { PropertyNamingStrategy.LOWER_CASE_WITH_DASHES, "lower_dash", "lower_dash" }, new String[] { PropertyNamingStrategy.LOWER_CASE_WITH_DASHES, "lowerDash", "lower-dash" }, new String[] { PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES, "lower_under", "lower_under" }, new String[] { PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES, "lowerUnder", "lower_under" }, new String[] { PropertyNamingStrategy.UPPER_CAMEL_CASE, "fooBar", "FooBar" }, new String[] { PropertyNamingStrategy.UPPER_CAMEL_CASE_WITH_SPACES, "fooBar", "Foo Bar" }, }; }
private PropertyNamingStrategy camelCaseStrategy() { return propertyName -> Character.toUpperCase(propertyName.charAt(0)) + (propertyName.length() > 1 ? propertyName.substring(1) : ""); }
@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\"}"); }
/** * Create instance of class model. * * @param clazz Class to model. * @param customization Customization of the class parsed from annotations. * @param parentClassModel Class model of parent class. * @param propertyNamingStrategy Property naming strategy. */ public ClassModel(Class<?> clazz, ClassCustomization customization, ClassModel parentClassModel, PropertyNamingStrategy propertyNamingStrategy) { this.clazz = clazz; this.classCustomization = customization; this.parentClassModel = parentClassModel; this.propertyNamingStrategy = propertyNamingStrategy; }
/** * Gets naming conversion strategy by name, see {@link javax.json.bind.config.PropertyNamingStrategy} * * @param strategyName name not null * @return strategy to use for conversion */ public static PropertyNamingStrategy getStrategy(String strategyName) { return strategies.get(strategyName); }
/** * If customized by JsonbPropertyAnnotation, than is used, otherwise use strategy to translate. * Since this is cached for performance reasons strategy has to be consistent * with calculated values for same input. */ private String calculateReadWriteName(String readWriteName, PropertyNamingStrategy strategy) { return readWriteName != null ? readWriteName : strategy.translateName(propertyName); }
/** * Gets property naming strategy. * * @return Property naming strategy. */ public PropertyNamingStrategy getPropertyNamingStrategy() { return propertyNamingStrategy; }