下面列出了com.fasterxml.jackson.databind.node.JsonNodeFactory#withExactBigDecimals ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void readDelimitedData(String lookup, List<String> lines) {
Splitter splitter;
if (lookup.matches(".*\\.csv")) {
splitter = Splitter.on(",");
} else if (lookup.matches(".*\\.tsv")) {
splitter = Splitter.on("\t");
} else {
throw new IllegalArgumentException("Must have file with .csv, .tsv or .json suffix");
}
List<String> names = Lists.newArrayList(splitter.split(lines.get(0)));
JsonNodeFactory nf = JsonNodeFactory.withExactBigDecimals(false);
ArrayNode localData = nf.arrayNode();
for (String line : lines.subList(1, lines.size())) {
ObjectNode r = nf.objectNode();
List<String> fields = Lists.newArrayList(splitter.split(line));
Preconditions.checkState(names.size() == fields.size(), "Wrong number of fields, expected ", names.size(), fields.size());
Iterator<String> ix = names.iterator();
for (String field : fields) {
r.put(ix.next(), field);
}
localData.add(r);
}
data = localData;
}
@Override
public JsonNode sample() {
double step;
if (stepDistribution == null) {
step = rand.nextGaussian() * sd.sample().asDouble() + mean.sample().asDouble();
} else {
step = stepDistribution.sample().asDouble();
}
double newState = state.addAndGet(step);
if (verbose) {
ObjectNode r = new ObjectNode(JsonNodeFactory.withExactBigDecimals(false));
r.set("value", new DoubleNode(newState));
r.set("step", new DoubleNode(step));
return r;
} else {
return new DoubleNode(newState);
}
}
@Test
public void testPrimitives() throws Exception {
JsonNodeFactory jsonFactory = JsonNodeFactory.withExactBigDecimals(true);
JsonNode json = jsonFactory.objectNode()
.set("protocol", jsonFactory.objectNode()
.put("type", "multi-primary")
.put("backups", 2));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.body(json)
.when()
.post("lock/test-primitive")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.when()
.get("primitives")
.then()
.statusCode(200)
.body("test-primitive.type", equalTo("lock"));
given()
.spec(specs.get(1))
.when()
.get("lock")
.then()
.statusCode(200)
.body("test-primitive.type", equalTo("lock"));
}
@Test
public void testLock() throws Exception {
JsonNodeFactory jsonFactory = JsonNodeFactory.withExactBigDecimals(true);
JsonNode json = jsonFactory.objectNode()
.set("protocol", jsonFactory.objectNode()
.put("type", "multi-primary")
.put("backups", 2));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.body(json)
.when()
.post("atomic-lock/test-lock")
.then()
.statusCode(200);
given()
.spec(specs.get(0))
.when()
.post("atomic-lock/test-lock/lock")
.then()
.statusCode(200);
given()
.spec(specs.get(0))
.when()
.delete("atomic-lock/test-lock/lock")
.then()
.statusCode(200);
}
@Test
public void testSemaphore() throws Exception {
JsonNodeFactory jsonFactory = JsonNodeFactory.withExactBigDecimals(true);
JsonNode json = jsonFactory.objectNode()
.put("initial-capacity", 2)
.set("protocol", jsonFactory.objectNode()
.put("type", "multi-primary")
.put("backups", 2));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.body(json)
.when()
.post("semaphore/test-semaphore")
.then()
.statusCode(200);
given()
.spec(specs.get(0))
.when()
.get("semaphore/test-semaphore/permits")
.then()
.statusCode(200)
.body(equalTo("2"));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.when()
.post("semaphore/test-semaphore/acquire")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.contentType(ContentType.JSON)
.when()
.post("semaphore/test-semaphore/acquire")
.then()
.statusCode(200);
given()
.spec(specs.get(2))
.when()
.get("semaphore/test-semaphore/permits")
.then()
.body(equalTo("0"));
given()
.spec(specs.get(1))
.contentType(ContentType.JSON)
.when()
.post("semaphore/test-semaphore/release")
.then()
.statusCode(200);
given()
.spec(specs.get(0))
.when()
.get("semaphore/test-semaphore/permits")
.then()
.body(equalTo("1"));
given()
.spec(specs.get(1))
.when()
.get("semaphore/test-semaphore/permits")
.then()
.body(equalTo("1"));
}
@Test
public void testValue() throws Exception {
JsonNodeFactory jsonFactory = JsonNodeFactory.withExactBigDecimals(true);
JsonNode json = jsonFactory.objectNode()
.set("protocol", jsonFactory.objectNode()
.put("type", "multi-primary")
.put("backups", 2));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.body(json)
.when()
.post("atomic-value/test-value")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.when()
.get("atomic-value/test-value/value")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.contentType(ContentType.TEXT)
.body("Hello world!")
.when()
.post("atomic-value/test-value/value")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.when()
.get("atomic-value/test-value/value")
.then()
.statusCode(200)
.body(equalTo("Hello world!"));
}
@Test
public void testMap() throws Exception {
JsonNodeFactory jsonFactory = JsonNodeFactory.withExactBigDecimals(true);
ObjectNode json = jsonFactory.objectNode()
.put("null-values", false);
json.set("protocol", jsonFactory.objectNode()
.put("type", "multi-primary")
.put("backups", 2));
json.set("cache", jsonFactory.objectNode()
.put("enabled", true));
given()
.spec(specs.get(0))
.contentType(ContentType.JSON)
.body(json)
.when()
.post("atomic-map/test")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.when()
.get("atomic-map/test/foo")
.then()
.statusCode(200);
given()
.spec(specs.get(0))
.body("Hello world!")
.when()
.put("atomic-map/test/foo")
.then()
.statusCode(200);
given()
.spec(specs.get(1))
.when()
.get("atomic-map/test/foo")
.then()
.statusCode(200)
.assertThat()
.body("value", equalTo("Hello world!"));
}