PACT for a JSON array of integers - java

Got the following JSON:
{
"content": [
7,
8,
9,
10
],
"last": true,
"total_elements": 9,
"total_pages": 2,
"first": false,
"number_of_elements": 4,
"size": 5,
"number": 1,
"empty": false
}
and I would like to create a contract with PACT using the following code:
DslPart body = newJsonBody((root) -> {
root.array("consumer", a -> a.integerType().integerType().integerType())
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty");
}).build();
Although this is working, I really do not like the array contract using a concatenation of integerType.
My question is: is there a better way to indicate an array of X elements, which have to be of type Integer?

You can use minArrayLike.
#Test
public void test() {
DslPart body = newJsonBody((root) ->
root.minArrayLike("content", 4, PactDslJsonRootValue.integerType(1), 4)
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty"))
.build();
System.out.println(body.toString());
}
Produces
{
"content": [
1,
1,
1,
1
],
"number": 100,
"last": true,
"size": 100,
"total_elements": 100,
"total_pages": 100,
"number_of_elements": 100,
"first": true,
"empty": true
}

Related

Spring boot Controller json response has a field name "empty"

I have a GET endpoint in my application supposed to return :
{
"gameId": "41a483c4-6220-424a-a931-d9114a4f6748",
"pits": [
{
"id": 1,
"stones": 6
},
{
"id": 2,
"stones": 6
},
{
"id": 3,
"stones": 6
},
{
"id": 4,
"stones": 6
},
{
"id": 5,
"stones": 6
},
{
"id": 6,
"stones": 6
},
{
"id": 7,
"stones": 0
},
{
"id": 8,
"stones": 6
},
{
"id": 9,
"stones": 6
},
{
"id": 10,
"stones": 6
},
{
"id": 11,
"stones": 6
},
{
"id": 12,
"stones": 6
},
{
"id": 13,
"stones": 6
},
{
"id": 14,
"stones": 0
}
],
"playerTurn": null,
"currentPitIndex": 0
}
but instead it returns:
{
"id": "25f09303-b797-418f-a7e7-db0e5fa8631b",
"pits": [
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
}
],
"playerTurn": null,
"currentPitIndex": null
}
I am wondering what is "empty"?! and where is "id"!
would be much appreciated for any suggestion and help.
thank you
Does your class have a method called isEmpty()? Because most JSON marshalling frameworks add JSON properties for any method with 0 arguments that starts with get, or returns a boolean and starts with is. For the same reason id is probably missing - I'm guessing there is no getter for it. The top level gameId is probably called id because you have a getId() method, not a getGameId() method.
For most frameworks you can tweak this with annotations. For instance, with Jackon you can use #JsonIgnore to indicate a method should not be represented as a JSON property, and #JsonProperty can be used to a) add a custom name, or b) allow a field to be included as well. For other frameworks you should check their documentation.

JSONArray throws A JSONArray text must start with '[' at 1 [character 2 line 1] exception

The following JSON Object (a variable named json_object) is composed of two values
peripheral_devices
peripheral_tests
both of which are arrays:
{
"peripheral_devices": [
{
"_id": 1,
"active": -1,
"battery": "63",
"bt_firmware_version": "btv1.7.777",
"configured": 0,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323224,
"device_id": 72,
"discovered": 0,
"firmware_version": "v3.14",
"hardware_version": "null",
"icon": "icon_device_bp5",
"last_connect_date": 1640023710420,
"mac_address": "8C:DE:52:41:FC:57",
"model": "BP5 41FC57",
"name": "BP5",
"other_id": "-1",
"paired": -1,
"type_id": 1,
"update_date": 1635807323224,
"user_id": 13
},
{
"_id": 3,
"active": -1,
"battery": "90",
"bt_firmware_version": "1.0.0",
"configured": -1,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323239,
"device_id": 72,
"discovered": 0,
"firmware_version": "1.0.0",
"hardware_version": "5.0.0",
"icon": "icon_device_hs2s",
"last_connect_date": 1640022147928,
"mac_address": "00:4D:32:0C:B7:2C",
"model": "HS2S 11070",
"name": "HS2S",
"other_id": "-1",
"paired": -1,
"type_id": 3,
"update_date": 1635807323239,
"user_id": 13
}
],
"peripheral_tests": [
{
"_id": 199,
"consumer_id": 22,
"create_date": 1640020949760,
"end_date": 1640020949078,
"mood_id": -1,
"notes": "",
"start_date": 1640020939533,
"status_id": 1,
"type_id": 2,
"update_date": 0,
"user_id": -99
},
{
"_id": 198,
"consumer_id": 22,
"create_date": 1640020904183,
"end_date": -1,
"metric_bps": {,
"mood_id": -1,
"notes": "",
"start_date": 1640020863742,
"status_id": 1,
"type_id": 1,
"update_date": 0,
"user_id": -99
},
{
"_id": 197,
"consumer_id": 22,
"create_date": 1640020834664,
"end_date": 1640020828741,
"mood_id": -1,
"notes": "",
"start_date": 1640020822580,
"status_id": 1,
"type_id": 3,
"update_date": 0,
"user_id": -99
}
]
}
and derived as follows:
JSONObject json_object = new JSONObject(post_data);
Using org.json.JSONArray.JSONArray:
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_devices");
works as expected and a json array is parsed from json_object. However, using the same technique to get the second array in the object does not:
JSONArray json_tests = new JSONArray("peripheral_tests");
throws exception:
A JSONArray text must start with '[' at 1 [character 2 line 1]
Why? What am I not understanding?
You code
JSONArray json_tests = new JSONArray("peripheral_tests");
Take string in the constructor which is the JSON data and peripheral_tests is not JSON array, to get peripheral_tests you need to get it from json_object like
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_tests");
The input JSON is invalid, accoring to JSON Lint ...which may lead to unexpected results:
Error: Parse error on line 69:
..."metric_bps": { , "mood_id": -1,
----------------------^
Expecting 'STRING', '}', got ','

How to read this json with gson?

I have this Json and I don't know how to read when the atributtes are dynamic...
Note that example below, the field "atletas" is a List by "Ids"... How can I get this information if I don't know what Id will get?
For example:
{
"rodada": 2,
"atletas":
"100651": {
"apelido": "Rodrygo",
"pontuacao": 1.3,
"scout": {
"FC": 3,
"FF": 1,
"FS": 1,
"RB": 1
},
"foto": "https://s.glbimg.com/es/sde/f/2017/11/01/b8128d3d5db5325dc238cadc67c28342_FORMATO.png",
"posicao_id": 5,
"clube_id": 277
},
"101957": {
"apelido": "Thiago Larghi",
"pontuacao": 0,
"scout": {
},
"foto": "https://s.glbimg.com/es/sde/f/2018/03/19/b1b3b42713071408fb76d25cfb76d927_FORMATO.jpeg",
"posicao_id": 6,
"clube_id": 282
},
"36773": {
"apelido": "Julio Cesar",
"pontuacao": 8,
"scout": {
"DD": 1,
"SG": 1
},
"foto": "https://s.glbimg.com/es/sde/f/2018/04/17/326a0f6b72076eb12b4e6b335ae6a1da_FORMATO.png",
"posicao_id": 1,
"clube_id": 262
}
......
'

How to properly serialize spring-data PageImpl without using hateoas/data-rest module

I have a simple Spring Boot application that has the next method inside my controller:
#RequestMapping(method = RequestMethod.GET, value = "/")
public ResponseEntity<Page> getUsers(Pageable pageable) {
Page<User> users = repository.findAll(pageable);
return ResponseEntity.ok(users);
}
My records are being fetched using simple pageable repository. The response generated is:
{
"content": [
{
...
},
{
...
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 3,
"last": true,
"size": 20,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true
},
"numberOfElements": 3,
"first": true
}
How do I achieve same result as when I use ResourceSupport, which is:
.....
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
Reason is - I do not need the links generation and so do not want to deal with the ResourceSupport/Assembler facilities of hateoas.
Is there any built-in option (besides writing my own serialization for Page < T >) ?

How to implement for loop for JsonArray Data [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
Hi i am not able to do looping for the below multidimensional JsonArray Data which is coming from nodejs Server.
Even the index values of the jsonarray will also increase dynamically whenever more data will come into it. Please help me to implement for loop to get data dynamically.
Response which is i am getting from server is:-
[
[{
"id": 3,
"user_id": 22,
"coin": "btc",
"coin_quantity": 4.24524129,
"order": 1,
"order_price": 175,
"total_amount": 742.92,
"order_type": 0,
"processed": 3.85931026,
"remaining": 0.02425852,
"status": 1,
"t_fee_inr": 125.92,
"t_fee_coin": 0,
"t_gst": 22.66,
"invoice": null,
"create_time": "2018-03-20T21:22:49.000Z",
"complete_time": null
}, {
"id": 5,
"user_id": 22,
"coin": "btc",
"coin_quantity": 2.24524129,
"order": 1,
"order_price": 174.8,
"total_amount": 392.47,
"order_type": 0,
"processed": 0,
"remaining": 2.24524129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:41:19.000Z",
"complete_time": null
}, {
"id": 7,
"user_id": 22,
"coin": "btc",
"coin_quantity": 0.64524129,
"order": 1,
"order_price": 174.85,
"total_amount": 112.82,
"order_type": 0,
"processed": 0,
"remaining": 0.64524129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:42:08.000Z",
"complete_time": null
}, {
"id": 9,
"user_id": 22,
"coin": "btc",
"coin_quantity": 0.76324129,
"order": 1,
"order_price": 174.89,
"total_amount": 133.48,
"order_type": 0,
"processed": 0,
"remaining": 0.76324129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:43:07.000Z",
"complete_time": null
}],
[{
"total_buy_orders": 4
}], {
"page_no": "1"
}
]
You can try this way
JSONArray array = yourJsonArrayFromServer;
for(int i = 0 ; i < array.length(); i++){
// get current object with: array.getJSONObject(i)
// do something
}
If I understand well your question.
var test = []; //your array
for(let key of test){
if(key instanceof Array) {
console.log('array'); //detect if its an array then do iteration
getValues(key);
}else{
console.log(key); //value to access, its an object
}
}
function getValues(key){
for(let val of key){
console.log(val); //value to access, its an object
}
}
This will work for you using JQuery .each() function
$.each(json, function (key, data) {
$.each(data, function (index, data) {
console.log('index', data)
})
});
json is your JsonArray Data

Categories