blob: 7e1050e070d8d4767b7c7981d0adcd61cca09fc5 [file] [log] [blame]
henrike@webrtc.org47be73b2014-05-13 18:00:261/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Henrik Kjellanderfb3e1b62017-06-29 06:03:0411#ifndef WEBRTC_RTC_BASE_JSON_H_
12#define WEBRTC_RTC_BASE_JSON_H_
henrike@webrtc.org47be73b2014-05-13 18:00:2613
Henrik Kjellander88b2dd42017-06-29 05:52:5014#include <string>
15#include <vector>
henrike@webrtc.org47be73b2014-05-13 18:00:2616
Henrik Kjellander88b2dd42017-06-29 05:52:5017#if !defined(WEBRTC_EXTERNAL_JSON)
18#include "json/json.h"
19#else
20#include "third_party/jsoncpp/json.h"
21#endif
22
23namespace rtc {
24
25///////////////////////////////////////////////////////////////////////////////
26// JSON Helpers
27///////////////////////////////////////////////////////////////////////////////
28
29// Robust conversion operators, better than the ones in JsonCpp.
30bool GetIntFromJson(const Json::Value& in, int* out);
31bool GetUIntFromJson(const Json::Value& in, unsigned int* out);
32bool GetStringFromJson(const Json::Value& in, std::string* out);
33bool GetBoolFromJson(const Json::Value& in, bool* out);
34bool GetDoubleFromJson(const Json::Value& in, double* out);
35
36// Pull values out of a JSON array.
37bool GetValueFromJsonArray(const Json::Value& in, size_t n,
38 Json::Value* out);
39bool GetIntFromJsonArray(const Json::Value& in, size_t n,
40 int* out);
41bool GetUIntFromJsonArray(const Json::Value& in, size_t n,
42 unsigned int* out);
43bool GetStringFromJsonArray(const Json::Value& in, size_t n,
44 std::string* out);
45bool GetBoolFromJsonArray(const Json::Value& in, size_t n,
46 bool* out);
47bool GetDoubleFromJsonArray(const Json::Value& in, size_t n,
48 double* out);
49
50// Convert json arrays to std::vector
51bool JsonArrayToValueVector(const Json::Value& in,
52 std::vector<Json::Value>* out);
53bool JsonArrayToIntVector(const Json::Value& in,
54 std::vector<int>* out);
55bool JsonArrayToUIntVector(const Json::Value& in,
56 std::vector<unsigned int>* out);
57bool JsonArrayToStringVector(const Json::Value& in,
58 std::vector<std::string>* out);
59bool JsonArrayToBoolVector(const Json::Value& in,
60 std::vector<bool>* out);
61bool JsonArrayToDoubleVector(const Json::Value& in,
62 std::vector<double>* out);
63
64// Convert std::vector to json array
65Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in);
66Json::Value IntVectorToJsonArray(const std::vector<int>& in);
67Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in);
68Json::Value StringVectorToJsonArray(const std::vector<std::string>& in);
69Json::Value BoolVectorToJsonArray(const std::vector<bool>& in);
70Json::Value DoubleVectorToJsonArray(const std::vector<double>& in);
71
72// Pull values out of a JSON object.
73bool GetValueFromJsonObject(const Json::Value& in, const std::string& k,
74 Json::Value* out);
75bool GetIntFromJsonObject(const Json::Value& in, const std::string& k,
76 int* out);
77bool GetUIntFromJsonObject(const Json::Value& in, const std::string& k,
78 unsigned int* out);
79bool GetStringFromJsonObject(const Json::Value& in, const std::string& k,
80 std::string* out);
81bool GetBoolFromJsonObject(const Json::Value& in, const std::string& k,
82 bool* out);
83bool GetDoubleFromJsonObject(const Json::Value& in, const std::string& k,
84 double* out);
85
86// Writes out a Json value as a string.
87std::string JsonValueToString(const Json::Value& json);
88
89} // namespace rtc
Thiago Farina8be8c2d2015-04-02 09:59:1590
Henrik Kjellanderfb3e1b62017-06-29 06:03:0491#endif // WEBRTC_RTC_BASE_JSON_H_