libzypp 17.37.10

json.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_CORE_PARSER_JSON_PARSER_H
13#define ZYPP_CORE_PARSER_JSON_PARSER_H
14
15#include <optional>
16#include <zypp-core/parser/parseexception.h>
17#include <zypp-core/base/inputstream.h>
18#include <zypp-core/base/NonCopyable.h>
19#include <zypp-core/zyppng/pipelines/expected.h>
20#include "json/JsonValue.h"
21
22 namespace zypp::json {
23
24 class Parser : private base::NonCopyable
25 {
26 public:
28 Parser() = default;
29 Parser(const Parser &) = delete;
30 Parser(Parser &&) = delete;
31 Parser &operator=(const Parser &) = delete;
32 Parser &operator=(Parser &&) = delete;
33
35 virtual ~Parser(){}
36
40 zyppng::expected<Value> parse( const InputStream & input_r );
41
42
43 struct Token {
44
45 enum Type {
46 TOK_STRING = 0,
47 TOK_NUMBER_FLOAT,
48 TOK_NUMBER_UINT,
49 TOK_NUMBER_INT,
50 TOK_BOOL_TRUE,
51 TOK_BOOL_FALSE,
52 TOK_NULL,
53 TOK_LSQUARE_BRACKET,
54 TOK_RSQUARE_BRACKET,
55 TOK_LCURLY_BRACKET,
56 TOK_RCURLY_BRACKET,
57 TOK_COMMA,
58 TOK_COLON,
59 TOK_END, // end of document
60 TOK_COUNT
61 };
62
63 Type _type;
64 std::string _token;
65
66 static Token eof();
67 };
68
69 zyppng::expected<Token> nextToken();
70
71 private:
72
73 zyppng::expected<Object> parseObject();
74 zyppng::expected<Array> parseArray();
75 zyppng::expected<Value> parseValue();
76 zyppng::expected<Value> finishParseValue( Token begin );
77
78
79 zyppng::expected<Token> parseStringToken();
80 zyppng::expected<Token> parseNumberToken();
81 zyppng::expected<Token> parseBoolToken();
82 zyppng::expected<Token> parseNullToken();
83
84
85 std::istream::char_type popChar();
86 std::istream::char_type peekChar();
87 void consumeChar();
88
89 zyppng::expected<void> consumeString( const std::string &str );
90
91 static inline std::istream::char_type eofChar() {
92 return std::istream::traits_type::eof();
93 }
94
95 template <typename T = Token>
96 zyppng::expected<T> makeParseError( const std::string &message, exception_detail::CodeLocation &&loc )
97 {
98 return zyppng::expected<T>::error(::zypp::exception_detail::do_ZYPP_EXCPT_PTR( parser::ParseException( message ), std::move(loc) ));
99 }
100
101 std::optional<InputStream> _stream;
102 int _nestingDepth = 0; //< how deep is the nesting ( prevent memory overflow by too deep nesting )
103 };
104}
105#endif
JsonValue.h
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::json::Parser::parseNullToken
zyppng::expected< Token > parseNullToken()
Definition json.cc:293
zypp::json::Parser::parseBoolToken
zyppng::expected< Token > parseBoolToken()
Definition json.cc:302
zypp::json::Parser::_nestingDepth
int _nestingDepth
Definition json.h:102
zypp::json::Parser::finishParseValue
zyppng::expected< Value > finishParseValue(Token begin)
Definition json.cc:265
zypp::json::Parser::parseObject
zyppng::expected< Object > parseObject()
Definition json.cc:87
zypp::json::Parser::parseNumberToken
zyppng::expected< Token > parseNumberToken()
Definition json.cc:323
zypp::json::Parser::operator=
Parser & operator=(const Parser &)=delete
zypp::json::Parser::consumeString
zyppng::expected< void > consumeString(const std::string &str)
Definition json.cc:496
zypp::json::Parser::parseValue
zyppng::expected< Value > parseValue()
Definition json.cc:252
zypp::json::Parser::parse
zyppng::expected< Value > parse(const InputStream &input_r)
Parse the stream.
Definition json.cc:37
zypp::json::Parser::nextToken
zyppng::expected< Token > nextToken()
Definition json.cc:508
zypp::json::Parser::parseArray
zyppng::expected< Array > parseArray()
Definition json.cc:184
zypp::json::Parser::Parser
Parser(Parser &&)=delete
zypp::json::Parser::~Parser
virtual ~Parser()
Dtor.
Definition json.h:35
zypp::json::Parser::Parser
Parser()=default
Default ctor.
zypp::json::Parser::Parser
Parser(const Parser &)=delete
zypp::json::Parser::eofChar
static std::istream::char_type eofChar()
Definition json.h:91
zypp::json::Parser::consumeChar
void consumeChar()
Definition json.cc:490
zypp::json::Parser::parseStringToken
zyppng::expected< Token > parseStringToken()
Definition json.cc:594
zypp::json::Parser::popChar
std::istream::char_type popChar()
Definition json.cc:466
zypp::json::Parser::peekChar
std::istream::char_type peekChar()
Definition json.cc:478
zypp::json::Parser::_stream
std::optional< InputStream > _stream
Definition json.h:101
zypp::json::Parser::makeParseError
zyppng::expected< T > makeParseError(const std::string &message, exception_detail::CodeLocation &&loc)
Definition json.h:96
zypp::json::Parser::operator=
Parser & operator=(Parser &&)=delete
zypp::parser::ParseException
Definition parseexception.h:34
zyppng::expected
Definition expected.h:27
zyppng::expected::error
E & error()
Definition expected.h:225
expected.h
inputstream.h
str
String related utilities and Regular expression matching.
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::exception_detail::do_ZYPP_EXCPT_PTR
std::exception_ptr do_ZYPP_EXCPT_PTR(TExcpt &&excpt_r, CodeLocation &&where_r)
Helper for ZYPP_EXCPT_PTR( Exception ).
Definition Exception.h:435
zypp::json
Definition TargetImpl.cc:150
parseexception.h
zypp::exception_detail::CodeLocation
Keep FILE, FUNCTION and LINE.
Definition Exception.h:36
zypp::json::Parser::Token
Definition json.h:43
zypp::json::Parser::Token::eof
static Token eof()
Definition json.cc:752
zypp::json::Parser::Token::_token
std::string _token
Definition json.h:64
zypp::json::Parser::Token::Type
Type
Definition json.h:45
zypp::json::Parser::Token::TOK_NUMBER_FLOAT
@ TOK_NUMBER_FLOAT
Definition json.h:47
zypp::json::Parser::Token::TOK_NULL
@ TOK_NULL
Definition json.h:52
zypp::json::Parser::Token::TOK_LSQUARE_BRACKET
@ TOK_LSQUARE_BRACKET
Definition json.h:53
zypp::json::Parser::Token::TOK_COUNT
@ TOK_COUNT
Definition json.h:60
zypp::json::Parser::Token::TOK_END
@ TOK_END
Definition json.h:59
zypp::json::Parser::Token::TOK_STRING
@ TOK_STRING
Definition json.h:46
zypp::json::Parser::Token::TOK_RSQUARE_BRACKET
@ TOK_RSQUARE_BRACKET
Definition json.h:54
zypp::json::Parser::Token::TOK_BOOL_FALSE
@ TOK_BOOL_FALSE
Definition json.h:51
zypp::json::Parser::Token::TOK_COMMA
@ TOK_COMMA
Definition json.h:57
zypp::json::Parser::Token::TOK_NUMBER_INT
@ TOK_NUMBER_INT
Definition json.h:49
zypp::json::Parser::Token::TOK_RCURLY_BRACKET
@ TOK_RCURLY_BRACKET
Definition json.h:56
zypp::json::Parser::Token::TOK_COLON
@ TOK_COLON
Definition json.h:58
zypp::json::Parser::Token::TOK_NUMBER_UINT
@ TOK_NUMBER_UINT
Definition json.h:48
zypp::json::Parser::Token::TOK_BOOL_TRUE
@ TOK_BOOL_TRUE
Definition json.h:50
zypp::json::Parser::Token::TOK_LCURLY_BRACKET
@ TOK_LCURLY_BRACKET
Definition json.h:55
zypp::json::Parser::Token::_type
Type _type
Definition json.h:63
NonCopyable.h