Stroika Library 3.0d20
 
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 class 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 using Algorithm::eStroikaNative;
47#if __has_include("boost/json.hpp")
48 using Algorithm::eBoost;
49#endif
50
51 optional<Algorithm> fPreferredAlgorithm;
52 };
53 using ReaderOptions::Algorithm::eStroikaNative;
54#if __has_include("boost/json.hpp")
55 using ReaderOptions::Algorithm::eBoost;
56#endif
57
58 /**
59 * \note Our definition of the JSON format comes from:
60 * http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
61 *
62 * There are several variations. Please also note that http://jsonlint.com/ disallows
63 * json top level values other than array or object, but that doesn't appear to follow
64 * the standard as specified in ECMA.
65 *
66 * Another good summary is http://json.org/
67 *
68 * \note Any JSON object can be represented as as VariantValue object, and unambiguously mapped back to the same JSON.
69 *
70 * \note Aliases: JSONReader, JSON Reader, JSON-Reader.
71 *
72 * \note If the inputStream which is 'Read' is seekable, it will automatically be positioned to the end
73 * of the argument JSON on successful exit. However, if its not seekable, it will be left in an arbitrary
74 * location (possibly well past the end of the valid JSON).
75 *
76 * \par Example Usage
77 * \code
78 * string srcJSON = "[101]"; // this srcJSON can be a stream, String, string or any of a number of sources (@see DataExchange::Variant::Read)
79 * VariantValue v1 = DataExchange::Variant::JSON::Reader{}.Read (srcJSON);
80 * // now unpack the result of the variant-value, with v1.GetType, v1.As<Sequence<VariantValue>> () etc...
81 * \endcode
82 */
83 class Reader : public Variant::Reader {
84 private:
86
87 public:
88 Reader (const ReaderOptions& options = {});
89
90 private:
91 class NativeRep_;
92#if __has_include("boost/json.hpp")
93 class BoostRep_;
94#endif
95 static shared_ptr<_IRep> mk_ (const ReaderOptions& options);
96 };
97
98}
99
100/*
101 ********************************************************************************
102 ***************************** Implementation Details ***************************
103 ********************************************************************************
104 */
105
106#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 ...