What to do if the JSONHint annotation does not work with lombok and JSONIC

A note about the workaround because the `` `@ JSONHint``` annotation did not work well when using JSONIC and lombok together.

Version used

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

<dependency>
    <groupId>net.arnx</groupId>
    <artifactId>jsonic</artifactId>
    <version>1.3.10</version>
</dependency>

Phenomenon and workaround

Suppose you want to parse a JSON that contains a date string in the following format as a sample.

String json = "{dateTime: \"2018-03-29T11:22:33.123+09:00\"}";
SampleData s = JSON.decode(json, SampleData.class);

A class that uses lombok looks like this.

import java.time.LocalDateTime;

import lombok.Data;
import net.arnx.jsonic.JSONHint;

@Data
public class SampleData {
	@JSONHint(format="yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ")
	private LocalDateTime dateTime;
}

This is an exception as shown below, and you can see that the `` `@ JSONHint``` annotation is not enabled.

Exception in thread "main" net.arnx.jsonic.JSONException: {dateTime=2018-03-29T11:22:33.123+09:00}Is class asdf.asdfasd.Could not convert to SampleData: $.dateTime
	at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1775)
	at net.arnx.jsonic.JSON.parse(JSON.java:1155)
	at net.arnx.jsonic.JSON.parse(JSON.java:1130)
	at net.arnx.jsonic.JSON.decode(JSON.java:665)
	at asdf.asdfasd.App.main(App.java:15)
Caused by: java.time.format.DateTimeParseException: Text '2018-03-29T11:22:33.123+09:00' could not be parsed, unparsed text found at index 23
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)

The cause of this is that https://ja.osdn.net/projects/jsonic/ticket/35184 states that if there is a getter / setter, those annotations will take precedence. lombok automatically generates getters / setters, but since there is no `` `@ JSONHint``` there, this happens.

So, as a workaround, I decided to define getter / setter for the target property by myself.

@Data
public class SampleData {
	/*Getter for this property/Since setter is unnecessary, automatic generation is suppressed*/
	@Getter(AccessLevel.NONE)
	@Setter(AccessLevel.NONE)
	private LocalDateTime d;

	public LocalDateTime getDateTime() {
		return d;
	}

	@JSONHint(format="yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ")
	public void setDateTime(LocalDateTime dateTime) {
		this.d = dateTime;
	}
}

Recommended Posts

What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if the JSONHint annotation does not work with lombok and JSONIC
Do not use floats and doubles to calculate decimal numbers
What to do if the breakpoint is shaded and does not stop during debugging
What to do if validation doesn't work with the update action
What to do if you installed Ruby with rbenv but the version does not change
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
What to do if the app is not created with the latest Rails version installed when rails new
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
What to do if the server tomcat dies
What to do if the changes are not reflected in the jar manifest file
What to do when rails db: seed does not reflect in the database
[Grails] Error occurred running What to do when the Grails CLI does not start
What to do when is invalid because it does not start with a'-'
What to do if the debug gem installation fails
What to do if the Rails server can't start
What to do if TextToSpeech doesn't work on Android 11
What to do when routing settings do not work after building Docker environment with Laravel
What to do if you cannot execute with the command "Java package name / class name"
What to do if audio is not available on discordrb
What to do if FacesMessage is set but not displayed
What to do if the adb command cannot be executed
What to do if you can't use the rails command
What to do if you change the Listen Address from the settings screen with Mattermost docker
[Rails] What to do if you can't get an error message with the errors method
What to do when Rails on Docker does not reflect controller changes in the browser
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
If the DB container does not start with Initializing database files
[Rails] What to do if data is not registered in DB
What to do if Intel RST (Rapid Storage Technology) interferes with dual booting Ubuntu20.04LTS and Windows10
What to do when the changes in the Servlet are not reflected
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
What to do if you get angry with OpenSSL with pyenv install
[Rails 5] [Turbolinks] What to do when JS does not work due to page transition or browser back
[Logback] What to do if unnecessary files do not disappear during rotation
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
[Rails] What to do if you can't get parameters with form_with
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What to do if JavaMail cannot be used with OpenJDK 11 or later
How to interact with a server that does not crash the app
[IOS] What to do when the image is filled with one color
What to do if changes are not reflected after automatic deployment to EC2
Add gem'rails-i18n','~> 6.0.0' and what to do if bundle install gives an error
What to do if you can't install the plugin from the Eclipse marketplace
What to do if deployment fails on Heroku (Ruby app not detected)
What to do if you install Ubuntu
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
How to make @Transactional work that does not work if you use it incorrectly
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
If the ctrl + shortcut does not work in Eclipse, disabling welcome may help
What to do if the rails server doesn't run out on AWS cloud9