2

when using the javax json ObjectBuilder to build json, the output json string contains "value" and "num" in the json string. my web service is throw serialization error when the json contains "value" and "num". any one know why the output have "value" and "num"?

example:

JsonObject addProductRequest = Json.createObjectBuilder()
            .add("OrderID", orderId)
            .add("Product", productId)
            .add("Quantity", qty)
            .add("access_token", access_token)
            .build();

output:

{
  "OrderID": {
    "num": 15498
  },
  "ProductID": {
    "num": 20
  },
  "Quantity": {
    "num": 1
  },
  "access_token": {
    "value": "1f6c689d-6956-4f8e-b259-9030d57bec4b"
  }
}

when I switch to using google.gson.JsonObject, the output dont have the "value" and "num" string, which my web service accepts and every thing seems to work fine. example

 com.google.gson.JsonObject addProductRequest = new com.google.gson.JsonObject();
    addProductRequest.addProperty("OrderID", orderId);
    addProductRequest.addProperty("Product", productId);
    addProductRequest.addProperty("Quantity", qty);
    addProductRequest.addProperty("access_token", access_token);

output:

{  "OrderID": 15499,  "Product": 20,  "Quantity": 1,  "access_token": "3241cfd4-7b6c-4eac-b2bb-9b2b0c780831"}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
satuday
  • 165
  • 1
  • 1
  • 8
  • What are you serializing? Show us the POJO and show us the code. – Sotirios Delimanolis Jul 01 '15 at 01:19
  • updated with code. there is no POCO, just using the builder. – satuday Jul 01 '15 at 01:25
  • And where are you getting the resulting `String` value? Or how do you output `addProductRequest` ? – Sotirios Delimanolis Jul 01 '15 at 01:26
  • the result string ouputed by rest-assured log. – satuday Jul 01 '15 at 01:31
  • You'd have to check how rest-assured logs it. Calling `addProductRequest.toString()` returns the expected json `String` value for me, with the glassfish 1.0.4 implementation of `javax.json`. – Sotirios Delimanolis Jul 01 '15 at 01:33
  • please insert how you fill the data and print the result – Safwan Hijazi Jul 01 '15 at 02:18
  • the problem seems to be from the RequestSpecification body. if I set the body with a com.google.gson.JsonObject object, rest assured will send the body in the correct format(with out "value" and "num"). but if I set the body with a javax.json.JsonObject object, there seems to be conversion problem, the body is not in the correct format(with "value" and "num"). example .body(object, ObjectMapperType.GSON) – satuday Jul 01 '15 at 02:43

1 Answers1

1

Rest Assured seems to use Gson to serialize POJOs (which is what you should be using as response entities) to the response body.

Gson doesn't know anything about javax.json. The implementation of javax.json that you are using basically has this format for its JsonObject:

private static final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject {
    private final Map<String, JsonValue> valueMap;      // unmodifiable

Since this is a Map, Gson uses special serializing, iterating through its entrySet and using each Entry as a JSON key-value pair (within a JSON object).

In this case, the entrySet returns

@Override
public Set<Entry<String, JsonValue>> entrySet() {
    return valueMap.entrySet();
} 

where the valueMap contains all the values you added with add in your builder. For example, for

.add("OrderID", 1)

it will have added an entry with the String key OrderID and the JsonValue value of

// Optimized JsonNumber impl for int numbers.
private static final class JsonIntNumber extends JsonNumberImpl {
    private final int num;

Notice the num field.

So Gson will now see a value of this JsonIntNumber type. It considers it a POJO and so serializes it as a JSON object with its fields as key-value pairs. It therefore produces

{
    "num": 15498
}

com.google.gson.JsonObject is a known type to Gson. It knows that it is special and not a custom POJO type. It can therefore write it directly, instead of having to serialize it further.


This is similar to a question which I answered here:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724