use of com.fasterxml.jackson.databind.JsonNode in project buck by facebook.
the class TargetsCommandIntegrationTest method testJsonOutputWithShowFullOutput.
@Test
public void testJsonOutputWithShowFullOutput() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "output_path", tmp);
workspace.setUp();
ProcessResult result = workspace.runBuckCommand("targets", "--json", "--show-full-output", "//:test");
ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
// Parse the observed JSON.
JsonNode observed = objectMapper.readTree(objectMapper.getFactory().createParser(result.getStdout()));
assertTrue(observed.isArray());
JsonNode targetNode = observed.get(0);
assertTrue(targetNode.isObject());
JsonNode cellPath = targetNode.get("buck.outputPath");
assertNotNull(cellPath);
Path expectedPath = tmp.getRoot().resolve("buck-out/gen/test/test-output");
String expectedRootPath = MorePaths.pathWithPlatformSeparators(expectedPath);
assertEquals(expectedRootPath, cellPath.asText());
}
use of com.fasterxml.jackson.databind.JsonNode in project Reader by TheKeeperOfPie.
the class ControllerComments method loadNestedComments.
public void loadNestedComments(final Comment moreComment) {
setLoading(true);
String children = "";
List<String> childrenList = moreComment.getChildren();
if (childrenList.isEmpty()) {
int commentIndex = listingComments.getChildren().indexOf(moreComment);
if (commentIndex >= 0) {
listingComments.getChildren().remove(commentIndex);
eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.REMOVE, commentIndex + 1));
}
return;
}
for (String id : childrenList) {
children += id + ",";
}
reddit.moreChildren(link.getName(), children.substring(0, children.length() - 1)).subscribe(new Observer<String>() {
@Override
public void onCompleted() {
setLoading(false);
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(String response) {
Log.d(TAG, "onNext() called with: " + "response = [" + response + "]");
try {
JsonNode nodeThings = ComponentStatic.getObjectMapper().readValue(response, JsonNode.class).get("json").get("data").get("things");
Listing listing = new Listing();
List<Thing> things = new ArrayList<>();
List<Thing> comments = new ArrayList<>();
for (JsonNode node : nodeThings) {
Comment comment = Comment.fromJson(node, moreComment.getLevel());
// For some reason Reddit doesn't report the link author, so we'll do it manually
comment.setLinkAuthor(link.getAuthor());
if (comment.getParentId().equals(link.getId())) {
comments.add(comment);
} else {
// TODO: Find a starting index to insert comments, without iterating the entire data list so many times
int commentIndex = -1;
for (int position = 0; position < comments.size(); position++) {
if (comments.get(position).getId().equals(comment.getParentId())) {
commentIndex = position;
break;
}
}
if (commentIndex >= 0) {
comment.setLevel(((Comment) comments.get(commentIndex)).getLevel() + 1);
}
comments.add(commentIndex + 1, comment);
}
}
if (comments.isEmpty()) {
int commentIndex = link.getComments().getChildren().indexOf(moreComment);
if (commentIndex >= 0) {
link.getComments().getChildren().remove(commentIndex);
}
commentIndex = listingComments.getChildren().indexOf(moreComment);
if (commentIndex >= 0) {
listingComments.getChildren().remove(commentIndex);
eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.REMOVE, commentIndex + 1));
}
} else {
things.addAll(comments);
listing.setChildren(things);
insertComments(moreComment, listing);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
use of com.fasterxml.jackson.databind.JsonNode in project Reader by TheKeeperOfPie.
the class ActivityMain method loadAccountInfo.
private void loadAccountInfo() {
boolean visible = controllerUser.hasUser();
if (visible) {
reddit.me().flatMap(UtilsRx.flatMapWrapError(response -> ComponentStatic.getObjectMapper().readValue(response, JsonNode.class))).subscribe(new ObserverNext<JsonNode>() {
@Override
public void onNext(JsonNode jsonNode) {
textAccountName.setText(UtilsJson.getString(jsonNode.get("name")));
textAccountInfo.setText(getString(R.string.account_info, UtilsJson.getString(jsonNode.get("link_karma")), UtilsJson.getString(jsonNode.get("comment_karma"))));
}
});
} else {
textAccountName.setText(R.string.login);
textAccountInfo.setText("");
}
MenuItem itemInbox = viewNavigation.getMenu().findItem(R.id.item_inbox);
itemInbox.setVisible(visible);
itemInbox.setEnabled(visible);
}
use of com.fasterxml.jackson.databind.JsonNode in project Reader by TheKeeperOfPie.
the class ControllerSearch method reloadSubscriptionList.
public void reloadSubscriptionList() {
Listing listing = new Listing();
String subscriptionsJson = preferences.getString(AppSettings.SUBSCRIPTIONS + controllerUser.getUser().getName(), "");
Log.d(TAG, "subscriptionsJson: " + subscriptionsJson);
if (!TextUtils.isEmpty(subscriptionsJson)) {
try {
JsonNode jsonNode = ComponentStatic.getObjectMapper().readValue(subscriptionsJson, JsonNode.class);
for (JsonNode node : jsonNode) {
listing.getChildren().add(Subreddit.fromJson(node));
}
} catch (IOException e) {
e.printStackTrace();
}
}
subredditsSubscribed = listing;
if (TextUtils.isEmpty(query)) {
subreddits = subredditsSubscribed;
for (Listener listener : listeners) {
listener.getAdapterSearchSubreddits().notifyDataSetChanged();
}
}
String url;
if (controllerUser.hasUser()) {
url = Reddit.OAUTH_URL + "/subreddits/mine/subscriber";
} else {
url = Reddit.OAUTH_URL + "/subreddits/default";
}
reddit.subreddits(url, null, 100).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new Observer<Listing>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Listing listing) {
subredditsLoaded.addChildren(listing.getChildren());
subredditsLoaded.setAfter(listing.getAfter());
if (listing.getChildren().size() == 100) {
loadMoreSubscriptions();
} else {
loadContributorSubreddits();
}
}
});
}
use of com.fasterxml.jackson.databind.JsonNode in project Reader by TheKeeperOfPie.
the class Comment method fromJson.
public static Comment fromJson(JsonNode nodeRoot, int level) {
Comment comment = new Comment();
comment.setJson(nodeRoot.toString());
comment.setLevel(level);
comment.setKind(UtilsJson.getString(nodeRoot.get("kind")));
JsonNode jsonNode = nodeRoot.get("data");
String id = UtilsJson.getString(jsonNode.get("id"));
int indexStart = id.indexOf("_");
if (indexStart >= 0) {
comment.setId(id.substring(indexStart + 1));
} else {
comment.setId(id);
}
comment.setName(UtilsJson.getString(jsonNode.get("name")));
String parentId = UtilsJson.getString(jsonNode.get("parent_id"));
indexStart = parentId.indexOf("_");
if (indexStart >= 0) {
comment.setParentId(parentId.substring(indexStart + 1));
} else {
comment.setParentId(parentId);
}
if (comment.getKind().equals("more")) {
comment.setIsMore(true);
comment.setCount(UtilsJson.getInt(jsonNode.get("count")));
List<String> children = new LinkedList<>();
for (JsonNode node : jsonNode.get("children")) {
children.add(UtilsJson.getString(node));
}
comment.setChildren(children);
return comment;
}
// Timestamps multiplied by 1000 as Java uses milliseconds and Reddit uses seconds
comment.setCreated(UtilsJson.getLong(jsonNode.get("created")) * 1000);
comment.setCreatedUtc(UtilsJson.getLong(jsonNode.get("created_utc")) * 1000);
comment.setApprovedBy(UtilsJson.getString(jsonNode.get("approved_by")));
comment.setAuthor(UtilsJson.getString(jsonNode.get("author")));
comment.setAuthorFlairCssClass(UtilsJson.getString(jsonNode.get("author_flair_css_class")));
comment.setAuthorFlairText(UtilsJson.getString(jsonNode.get("author_flair_text")));
comment.setBannedBy(UtilsJson.getString(jsonNode.get("banned_by")));
comment.setBody(Html.fromHtml(UtilsJson.getString(jsonNode.get("body")).replaceAll("\n", "<br>")));
comment.setBodyHtml(UtilsReddit.getFormattedHtml(UtilsJson.getString(jsonNode.get("body_html"))));
switch(UtilsJson.getString(jsonNode.get("distinguished"))) {
case "moderator":
comment.setDistinguished(Reddit.Distinguished.MODERATOR);
break;
case "admin":
comment.setDistinguished(Reddit.Distinguished.ADMIN);
break;
case "special":
comment.setDistinguished(Reddit.Distinguished.SPECIAL);
break;
default:
comment.setDistinguished(Reddit.Distinguished.NOT_DISTINGUISHED);
break;
}
String edited = UtilsJson.getString(jsonNode.get("edited"));
switch(edited) {
case "true":
comment.setEdited(1);
break;
case "false":
comment.setEdited(0);
break;
default:
comment.setEdited(UtilsJson.getLong(jsonNode.get("edited")) * 1000);
break;
}
comment.setGilded(UtilsJson.getInt(jsonNode.get("gilded")));
switch(UtilsJson.getString(jsonNode.get("likes"))) {
case "null":
comment.setLikes(Likes.NONE);
break;
case "true":
comment.setLikes(Likes.UPVOTE);
break;
case "false":
comment.setLikes(Likes.DOWNVOTE);
break;
}
comment.setLinkId(UtilsJson.getString(jsonNode.get("link_id")));
comment.setNumReports(UtilsJson.getInt(jsonNode.get("num_reports")));
comment.setSaved(UtilsJson.getBoolean(jsonNode.get("saved")));
comment.setScore(UtilsJson.getInt(jsonNode.get("score")));
comment.setScoreHidden(UtilsJson.getBoolean(jsonNode.get("score_hidden")));
comment.setSubreddit(UtilsJson.getString(jsonNode.get("subreddit")));
comment.setSubredditId(UtilsJson.getString(jsonNode.get("subreddit_id")));
comment.setLinkAuthor(UtilsJson.getString(jsonNode.get("link_author")));
comment.setLinkTitle(UtilsJson.getString(jsonNode.get("link_title")));
comment.setLinkUrl(UtilsJson.getString(jsonNode.get("link_url")));
comment.setIsNew(UtilsJson.getBoolean(jsonNode.get("new")));
comment.setDest(UtilsJson.getString(jsonNode.get("dest")));
comment.setContext(UtilsJson.getString(jsonNode.get("context")));
return comment;
}
Aggregations