rippled
Loading...
Searching...
No Matches
tests/libxrpl/json/Output.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/json/Output.h>
21#include <xrpl/json/json_reader.h>
22#include <xrpl/json/json_writer.h>
23
24#include <doctest/doctest.h>
25
26#include <string>
27
28using namespace ripple;
29using namespace Json;
30
31TEST_SUITE_BEGIN("JsonOutput");
32
33static void
34checkOutput(std::string const& valueDesc)
35{
36 std::string output;
37 Json::Value value;
38 REQUIRE(Json::Reader().parse(valueDesc, value));
39 auto out = stringOutput(output);
40 outputJson(value, out);
41
42 auto expected = Json::FastWriter().write(value);
43 CHECK(output == expected);
44 CHECK(output == valueDesc);
45 CHECK(output == jsonAsString(value));
46}
47
48TEST_CASE("output cases")
49{
50 checkOutput("{}");
51 checkOutput("[]");
52 checkOutput(R"([23,4.25,true,null,"string"])");
53 checkOutput(R"({"hello":"world"})");
54 checkOutput("[{}]");
55 checkOutput("[[]]");
56 checkOutput(R"({"array":[{"12":23},{},null,false,0.5]})");
57}
58
59TEST_SUITE_END();
Outputs a Value in JSON format without formatting (not human friendly).
Definition json_writer.h:54
std::string write(Value const &root) override
Unserialize a JSON document into a Value.
Definition json_reader.h:39
Represents a JSON value.
Definition json_value.h:149
JSON (JavaScript Object Notation).
Definition json_errors.h:25
Output stringOutput(std::string &s)
std::string jsonAsString(Json::Value const &)
Return the minimal string representation of a Json::Value in O(n) time.
void outputJson(Json::Value const &, Output const &)
Writes a minimal representation of a Json value to an Output in O(n) time.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
T parse(T... args)