blob: 22b08a02f12d87a79d6b8cd234386bcde968159c [file] [log] [blame]
Yuta HIGUCHI46e30cc2017-02-02 12:29:34 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI46e30cc2017-02-02 12:29:34 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cli;
17
18import java.io.IOException;
19import org.apache.karaf.shell.commands.Command;
20
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.SerializationFeature;
23
24/**
25 * Pretty print previous command output JSON.
26 */
27@Command(scope = "onos", name = "pp",
28 description = "Pretty print JSON output from previous command")
29public class PrettyJson extends AbstractShellCommand {
30
31 @Override
32 protected void execute() {
33 try {
34 ObjectMapper mapper = new ObjectMapper();
35 mapper.enable(SerializationFeature.INDENT_OUTPUT);
36
37 String json = mapper.writerWithDefaultPrettyPrinter()
38 .writeValueAsString(mapper.readTree(System.in));
39
40 print("%s", json);
41 } catch (IOException e) {
42 log.error("Failed parsing JSON.", e);
43 print("%s", e);
44 }
45 }
46
47}