I want to add properties to an existing instance of JsonObject
. If this property is boolean
, this is quite easy:
我想将属性添加到现有的JsonObject实例。如果此属性是布尔值,这很容易:
JsonObject jo = ....;
jo.put("booleanProperty", JsonValue.TRUE);
However, I also want to add a JsonNumber
but I couldn't find a way to create an instance of JsonNumber
. Here's what I could do:
但是,我还想添加一个JsonNumber但是我找不到创建JsonNumber实例的方法。这是我能做的:
JsonObjectBuilder job = Json.createObjectBuilder();
JsonNumber jn = job.add("number", 42).build().getJsonNumber("number");
jo.put("numberProperty", jn);
But I couldn't think of a more dirty way to accomplish my task. So - is there are more direct, cleaner approach to add a JsonNumber
to an existing instance of JsonObject
?
但我想不出一个更脏的方式来完成我的任务。那么 - 是否有更直接,更清晰的方法将JsonNumber添加到现有的JsonObject实例中?
2 个解决方案
#1
22
Okay, I just figured it out myself: You can't.
好吧,我只是想出了自己:你做不到。
JsonObject
is supposed to be immutable. Even if JsonObject.put(key, value)
exists, at runtime this will throw an UnsupportedOperationException
. So if you want to add a key/value-pair to an existing JsonObject
you'll need something like
JsonObject应该是不可变的。即使JsonObject.put(key,value)存在,在运行时也会抛出UnsupportedOperationException。因此,如果要为现有的JsonObject添加键/值对,则需要类似的东西
private JsonObjectBuilder jsonObjectToBuilder(JsonObject jo) {
JsonObjectBuilder job = Json.createObjectBuilder();
for (Entry<String, JsonValue> entry : jo.entrySet()) {
job.add(entry.getKey(), entry.getValue());
}
return job;
}
and then use it with
然后用它
JsonObject jo = ...;
jo = jsonObjectToBuilder(jo).add("numberProperty", 42).build();
#2
0
JsonObject is immutable but can be copied into a JsonObjecBuilder using lambdas.
JsonObject是不可变的,但可以使用lambdas复制到JsonObjecBuilder中。
JsonObject source = ...
JsonObjectBuilder target = Json.createObjectBuilder();
source.forEach(target::add); // copy source into target
target.add("name", "value"); // add or update values
JsonObject destination = target.build(); // build destination
#1
22
Okay, I just figured it out myself: You can't.
好吧,我只是想出了自己:你做不到。
JsonObject
is supposed to be immutable. Even if JsonObject.put(key, value)
exists, at runtime this will throw an UnsupportedOperationException
. So if you want to add a key/value-pair to an existing JsonObject
you'll need something like
JsonObject应该是不可变的。即使JsonObject.put(key,value)存在,在运行时也会抛出UnsupportedOperationException。因此,如果要为现有的JsonObject添加键/值对,则需要类似的东西
private JsonObjectBuilder jsonObjectToBuilder(JsonObject jo) {
JsonObjectBuilder job = Json.createObjectBuilder();
for (Entry<String, JsonValue> entry : jo.entrySet()) {
job.add(entry.getKey(), entry.getValue());
}
return job;
}
and then use it with
然后用它
JsonObject jo = ...;
jo = jsonObjectToBuilder(jo).add("numberProperty", 42).build();
#2
0
JsonObject is immutable but can be copied into a JsonObjecBuilder using lambdas.
JsonObject是不可变的,但可以使用lambdas复制到JsonObjecBuilder中。
JsonObject source = ...
JsonObjectBuilder target = Json.createObjectBuilder();
source.forEach(target::add); // copy source into target
target.add("name", "value"); // add or update values
JsonObject destination = target.build(); // build destination