Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Variant/JSON/Reader.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_Variant_JSON_Reader_h_
5#define _Stroika_Foundation_DataExchange_Variant_JSON_Reader_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Common/Common.h"
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 *
19 * TODO:
20 * @todo Review \u style Unicode characters (such as "\uFDD0") parsing. Its mostly right, but
21 * some sloppiness about surrogates, versus 4-byte wchar_t, versus char16_, char32_t, etc.
22 *
23 * @todo Should be able to REDO code which currently uses wstring::iterator to use TextStream -
24 * its basically the same thing... (except for the issue of seekability)
25 */
26
27namespace Stroika::Foundation::DataExchange::Variant::JSON {
28
29 /**
30 */
31 struct ReaderOptions {
32 enum Algorithm {
33 eStroikaNative,
34#if __has_include("boost/json.hpp")
35 eBoost,
36#endif
37
38#if __has_include("boost/json.hpp")
39 eDEFAULT = eBoost,
40 Stroika_Define_Enum_Bounds (eStroikaNative, eBoost)
41#else
42 eDEFAULT = eStroikaNative,
43 Stroika_Define_Enum_Bounds (eStroikaNative, eStroikaNative)
44#endif
45 };
46
47 optional<Algorithm> fPreferredAlgorithm;
48 };
49 using ReaderOptions::Algorithm::eStroikaNative;
50#if __has_include("boost/json.hpp")
51 using ReaderOptions::Algorithm::eBoost;
52#endif
53
54 /**
55 * \note Our definition of the JSON format comes from:
56 * http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
57 *
58 * There are several variations. Please also note that http://jsonlint.com/ disallows
59 * json top level values other than array or object, but that doesn't appear to follow
60 * the standard as specified in ECMA.
61 *
62 * Another good summary is http://json.org/
63 *
64 * \note Any JSON object can be represented as as VariantValue object, and unambiguously mapped back to the same JSON.
65 *
66 * \note Aliases: JSONReader, JSON Reader, JSON-Reader.
67 *
68 * \note req inputStream.IsSeekable () - when reading an input stream
69 *
70 * \par Example Usage
71 * \code
72 * string srcJSON = "[101]"; // this srcJSON can be a stream, String, string or any of a number of sources (@see DataExchange::Variant::Read)
73 * VariantValue v1 = DataExchange::Variant::JSON::Reader{}.Read (srcJSON);
74 * // now unpack the result of the variant-value, with v1.GetType, v1.As<Sequence<VariantValue>> () etc...
75 * \endcode
76 */
77 class Reader : public Variant::Reader {
78 private:
80
81 public:
82 Reader (const ReaderOptions& options = {});
83
84 private:
85 class NativeRep_;
86#if __has_include("boost/json.hpp")
87 class BoostRep_;
88#endif
89 static shared_ptr<_IRep> mk_ (const ReaderOptions& options);
90 };
91
92}
93
94/*
95 ********************************************************************************
96 ***************************** Implementation Details ***************************
97 ********************************************************************************
98 */
99
100#endif /*_Stroika_Foundation_DataExchange_Variant_JSON_Reader_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
abstract class specifying interface for readers that map a source like XML or JSON to a VariantValue ...