JsonPath - reading Java Long type

I have JSON data that look like

{"SESSIONID": 7242750700467747000}

The number was previously obtained from the server response and is created by the server side as Java Long. The client identifies itself by thinking that it is sessionID and sends it with requests. The problem is that when a client request arrives at the server, I need to parse this value again to enter Long. I am using JsonPath in particular:

 <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path-assert</artifactId> <version>0.8.1</version> <scope>test</scope> </dependency> 

When I parse JSON data like this

Long sessionID = JsonPath.read (json, "$ .sessionID");

I get an exception:

java.lang.ClassCastException: java.lang.Integer cannot be passed to java.lang.Long

So it looks like the number is being processed by JsonPath as an Integer. This will undoubtedly lead to incorrect results, since Integer is less than Long. Is there a way in JsonPath to parse and return data like Long?

+4
source share
2 answers

Ok, I managed to change the JsonPath provider a bit. Now I use:

 <dependency> <groupId>com.jayway.restassured</groupId> <artifactId>rest-assured</artifactId> <version>1.7.2</version> </dependency> 

From there i can use

 import com.jayway.restassured.path.json.JsonPath; // ... Long sessionID = JsonPath.with(json).getLong("sessionID"); 

The designations in this library are the same, with the exception of the absence of $. in the beginning, which I do not find at all.

+5
source

For what I saw, the API does not provide this option.

So, I see two options for you:

  • In JSON serialization, make sessionId a string. Read the line and then do Long parsing.
  • Use Jackson to read JSON. This will allow you to read Long.
+1
source

Source: https://habr.com/ru/post/1479269/


All Articles