Hjson cpp版安装及使用

Hjson cpp版安装及使用

  1. Hjson介绍
    关于Hjson的优点可以参照http://hjson.org/ , 大概意思就是便于人类阅读,可加注释,非标准json的写法等等。
  2. 安装前的准备
    需要安装Cmake和4.9.0以上版本的GCC,可以在网上找相关的安装教程
  3. 开始安装Hjson
    在http://hjson.org/download.html下载安装包并解压
    tar -xcvf hjson-cpp-1.2.1.tar.gz
    $ cd hjson-cpp-1.2.1/
    $ mkdir build
    $ cd build
    $ cmake … -DHJSON_ENABLE_TEST=ON -DHJSON_ENABLE_INSTALL=ON
    如果这步失败了,可能是cmake没有找到GCC的最新版本,执行如下命令(我安装的是4.9.4):
    export CC=/usr/local/bin/x86_64-unknown-linux-gnu-gcc-4.9.4
    export CXX=/usr/local/bin/x86_64-unknown-linux-gnu-g++
    再执行一次cmake … -DHJSON_ENABLE_TEST=ON -DHJSON_ENABLE_INSTALL=ON应该就OK了,会在build下生成Makefile文件;
    执行make和make install完成Hjson的安装;
  4. Hjson的使用
    在自己的任意工程目录下写个测试代码test.cpp:
#include<hjson.h>
#include<iostream>
#include<cstdio>
#include<fstream>
using namespace std;

static const char *_szDefaultConfig = R"(
{
  imageSource: NO DEFAULT
  showImages: true
  writeImages: true
  printFrameIndex: false
  printFrameRate: true
}
)";


Hjson::Value GetConfig(const char *szInputConfig) {
  Hjson::Value defaultConfig = Hjson::Unmarshal(_szDefaultConfig);

  Hjson::Value inputConfig;
  try {
    inputConfig = Hjson::Unmarshal(szInputConfig);
  } catch(std::exception e) {
    std::fprintf(stderr, "Error: Failed to unmarshal input config\n");
    std::fprintf(stdout, "Default config:\n");
    std::fprintf(stdout, _szDefaultConfig);

    return Hjson::Value();
  }

  return Hjson::Merge(defaultConfig, inputConfig);
}


int main() {

  // Now let's look at decoding Hjson data into Hjson::Value.
std::string sampleText = R"(
{
  # specify rate in requests/second
  rate: 1000
  array:
  [
    foo
    bar
  ]
}
)";
  ifstream in("hjson.txt", ios::binary);
    if(!in.is_open()) {
    cout << "Error opening file!" << endl;
    return 0;
  }
  in.seekg(0, ios::end);
  int size = in.tellg();
  in.seekg(0, ios::beg);
  char *buffer = new char [size];
  in.read(buffer, size);
  in.close();
  cout << "size : " << size << endl;
  // Decode. Throws Hjson::syntax_error on failure.
  Hjson::Value dat = Hjson::Unmarshal(sampleText.c_str(), sampleText.size());
  Hjson::Value cell = Hjson::Unmarshal(buffer, size);
  delete buffer;
  Hjson::Value dat1 =  GetConfig((const char*)sampleText.c_str());
  // Values can be assigned directly without casting.
  int rate = dat["rate"];
  printf("%d\n", rate);

  // Sometimes it's difficult for the compiler to know
  // what primitive type to convert the Hjson::Value to.
  // Then an explicit cast can be used.
  printf("%s\n", static_cast<const char*>(dat["array"][0]));
  printf("%s\n", static_cast<const char*>(dat["array"][1]));

  // To encode to Hjson with default options:
  Hjson::Value sampleMap;
  sampleMap["apple"] = 5;
  sampleMap["lettuce"] = 7;
  std::string hjson = Hjson::Marshal(sampleMap);
  // this is short for:
  // auto options = Hjson::DefaultOptions();
  // std::string hjson = Hjson::MarshalWithOptions(sampleMap, options);
  //printf("%s\n", hjson.c_str());
  for(auto it = sampleMap.begin(); it != sampleMap.end(); ++it){
    std::cout << "Key: " << it->first << " value: " << it->second.to_string() << std::endl;
  }
  for(int index = 0; index < int(dat["array"].size()); ++index){
    std::cout << dat["array"][index].to_string() << std::endl;
  }
  std::string merg = Hjson::Marshal(cell);
  printf("%s\n", merg.c_str());

  ofstream out("json.txt");
  if(!out)
    cout << "Open new file fail!" << endl;
  else
    out << merg << endl;
  out.close();
  return 0;
}

code中hjson.txt 文件如下:

#**************************************
{ 'version': 4
     'cell':[
      { CELL : 'addffffffffffffff',
        coordinates : #layer, pin_name, x_coord, y_coord
        [(M1, VDD, 0.223, 0.917),
         (M1, VDD, 0.255, 0.187)
        ],
      }
   ]
}

同时在该目录下编写cmake输入文件CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(hjson)
add_executable(testbin
test.cpp
)

set_target_properties(testbin PROPERTIES
  CXX_STANDARD 11
  CXX_STANDARD_REQUIRED YES
  CXX_EXTENSIONS NO
)

find_package(hjson REQUIRED)
target_link_libraries(testbin hjson)

执行cmake . //“.”代表在当前目录执行cmake
这是会生成一个Makefile文件,再make一下,测试程序的bin文件testbin就生成了,
./testbin 就能看到结果了。
好了,Hjson的安装和使用就介绍完了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值