to_json/
to_json.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5extern crate nss_certdata_parser;
6
7use std::fs::File;
8use std::env::args;
9use std::io::BufReader;
10
11use nss_certdata_parser::reader::RawObjectIter;
12use nss_certdata_parser::syntax::Value;
13
14fn main() {
15    for path in args().skip(1) {
16        println!("[");
17        for res_obj in RawObjectIter::new(BufReader::new(File::open(path).unwrap())) {
18            println!("   {{");
19            for (k, v) in res_obj.unwrap() {
20                let vj = match v {
21                    Value::Token(t, vv) => format!("{{ type: {:?}, value: {:?} }}", t, vv),
22                    Value::String(s) => format!("{:?}", s),
23                    // base64 would be a more usual (and compact) way
24                    // to embed an octet stream into JSON, but that
25                    // would need additional crate dependences.
26                    Value::Binary(b) => format!("{:?}", b).trim_matches('"').to_owned(),
27                };
28                println!("      {:?}: {},", k, vj);
29            }
30            println!("   }},");
31        }
32        println!("]");
33    }
34}