Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static AtomicInteger counter = new AtomicInteger(1);
public static final String user1JsonStart = "{\n"
+ " \"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack";
public static final String user1JsonEnd = "\" },\n"
+ " \"gender\" : \"MALE\",\n"
+ " \"verified\" : false,\n"
+ " \"userImage\" : \"Rm9vYmFyIQ==\"\n"
+ "}";
@Benchmark
public User jsonUnmashal() throws Exception {
ObjectMapper mapper = new ObjectMapper();
StringBuilder jsonUser = new StringBuilder();
//build new Json String
jsonUser.append(user1JsonStart).append(counter.incrementAndGet()).append(user1JsonEnd);
User user = mapper.readValue(jsonUser.toString(), User.class);
return user;
}
@lajospajtek
Copy link

lajospajtek commented Jul 27, 2015

@Setup
public void jsonUser(final StateContainer sc) {
    sc.jsonUser = user1JsonStart + counter.getAndIncrement() + user1JsonEnd;
}

@State(Scope.Benchmark)
public static class StateContainer {
    String jsonUser;
}

@Benchmark
public User jsonUnmashal(final StateContainer sc) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final User user = mapper.readValue(sc.jsonUser, User.class);
    return user;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment