blob: 39d7da648cf1e06fed970ad0cbbf0f70327100cf [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI46e30cc2017-02-02 12:29:34 -080021
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.SerializationFeature;
24
25/**
26 * Pretty print previous command output JSON.
27 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028@Service
Yuta HIGUCHI46e30cc2017-02-02 12:29:34 -080029@Command(scope = "onos", name = "pp",
30 description = "Pretty print JSON output from previous command")
31public class PrettyJson extends AbstractShellCommand {
32
33 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034 protected void doExecute() {
Yuta HIGUCHI46e30cc2017-02-02 12:29:34 -080035 try {
36 ObjectMapper mapper = new ObjectMapper();
37 mapper.enable(SerializationFeature.INDENT_OUTPUT);
38
39 String json = mapper.writerWithDefaultPrettyPrinter()
40 .writeValueAsString(mapper.readTree(System.in));
41
42 print("%s", json);
43 } catch (IOException e) {
44 log.error("Failed parsing JSON.", e);
45 print("%s", e);
46 }
47 }
48
49}