|
| 1 | +// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey |
| 2 | +// Please see LICENSE for license or visit https://github.com/taocpp/json/ |
| 3 | + |
| 4 | +#ifndef TAOCPP_JSON_INCLUDE_SAX_FROM_VALUE_HH |
| 5 | +#define TAOCPP_JSON_INCLUDE_SAX_FROM_VALUE_HH |
| 6 | + |
| 7 | +#include <stdexcept> |
| 8 | + |
| 9 | +#include "../value.hh" |
| 10 | + |
| 11 | +namespace tao |
| 12 | +{ |
| 13 | + namespace json |
| 14 | + { |
| 15 | + namespace sax |
| 16 | + { |
| 17 | + // SAX producer to generate events from a JSON value |
| 18 | + template< template< typename ... > class Traits, typename Consumer > |
| 19 | + void from_value( const basic_value< Traits > & v, Consumer & consumer ) |
| 20 | + { |
| 21 | + switch( v.type() ) { |
| 22 | + case type::UNINITIALIZED: |
| 23 | + throw std::logic_error( "unable to produce events from uninitialized values" ); |
| 24 | + case type::DISCARDED: |
| 25 | + throw std::logic_error( "unable to produce events from discarded values" ); |
| 26 | + case type::NULL_: |
| 27 | + consumer.null(); |
| 28 | + return; |
| 29 | + case type::BOOLEAN: |
| 30 | + consumer.boolean( v.unsafe_get_boolean() ); |
| 31 | + return; |
| 32 | + case type::SIGNED: |
| 33 | + consumer.number( v.unsafe_get_signed() ); |
| 34 | + return; |
| 35 | + case type::UNSIGNED: |
| 36 | + consumer.number( v.unsafe_get_unsigned() ); |
| 37 | + return; |
| 38 | + case type::DOUBLE: |
| 39 | + consumer.number( v.unsafe_get_double() ); |
| 40 | + return; |
| 41 | + case type::STRING: |
| 42 | + consumer.string( v.unsafe_get_string() ); |
| 43 | + return; |
| 44 | + case type::ARRAY: |
| 45 | + consumer.begin_array(); |
| 46 | + for( const auto & e : v.unsafe_get_array() ) { |
| 47 | + sax::from_value( e, consumer ); |
| 48 | + consumer.element(); |
| 49 | + } |
| 50 | + consumer.end_array(); |
| 51 | + return; |
| 52 | + case type::OBJECT: |
| 53 | + consumer.begin_object(); |
| 54 | + for( const auto & e : v.unsafe_get_object() ) { |
| 55 | + consumer.key( e.first ); |
| 56 | + sax::from_value( e.second, consumer ); |
| 57 | + consumer.member(); |
| 58 | + } |
| 59 | + consumer.end_object(); |
| 60 | + return; |
| 61 | + case type::RAW_PTR: |
| 62 | + if ( const basic_value< Traits > * p = v.unsafe_get_raw_ptr() ) { |
| 63 | + sax::from_value( * p, consumer ); |
| 64 | + } |
| 65 | + else { |
| 66 | + consumer.null(); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + throw std::logic_error( "invalid value for tao::json::type" ); // LCOV_EXCL_LINE |
| 71 | + } |
| 72 | + |
| 73 | + // SAX producer to generate events from an rvalue JSON value |
| 74 | + // note: strings from the source might be moved in the consumer |
| 75 | + template< template< typename ... > class Traits, typename Consumer > |
| 76 | + void from_value( basic_value< Traits > && v, Consumer & consumer ) |
| 77 | + { |
| 78 | + switch( v.type() ) { |
| 79 | + case type::UNINITIALIZED: |
| 80 | + throw std::logic_error( "unable to produce events from uninitialized values" ); |
| 81 | + case type::DISCARDED: |
| 82 | + throw std::logic_error( "unable to produce events from discarded values" ); |
| 83 | + case type::NULL_: |
| 84 | + consumer.null(); |
| 85 | + return; |
| 86 | + case type::BOOLEAN: |
| 87 | + consumer.boolean( v.unsafe_get_boolean() ); |
| 88 | + return; |
| 89 | + case type::SIGNED: |
| 90 | + consumer.number( v.unsafe_get_signed() ); |
| 91 | + return; |
| 92 | + case type::UNSIGNED: |
| 93 | + consumer.number( v.unsafe_get_unsigned() ); |
| 94 | + return; |
| 95 | + case type::DOUBLE: |
| 96 | + consumer.number( v.unsafe_get_double() ); |
| 97 | + return; |
| 98 | + case type::STRING: |
| 99 | + consumer.string( std::move( v.unsafe_get_string() ) ); |
| 100 | + return; |
| 101 | + case type::ARRAY: |
| 102 | + consumer.begin_array(); |
| 103 | + for( auto && e : v.unsafe_get_array() ) { |
| 104 | + sax::from_value( std::move( e ), consumer ); |
| 105 | + consumer.element(); |
| 106 | + } |
| 107 | + consumer.end_array(); |
| 108 | + return; |
| 109 | + case type::OBJECT: |
| 110 | + consumer.begin_object(); |
| 111 | + for( auto && e : v.unsafe_get_object() ) { |
| 112 | + consumer.key( std::move( e.first ) ); |
| 113 | + sax::from_value( std::move( e.second ), consumer ); |
| 114 | + consumer.member(); |
| 115 | + } |
| 116 | + consumer.end_object(); |
| 117 | + return; |
| 118 | + case type::RAW_PTR: |
| 119 | + if ( const basic_value< Traits > * p = v.unsafe_get_raw_ptr() ) { |
| 120 | + sax::from_value( * p, consumer ); |
| 121 | + } |
| 122 | + else { |
| 123 | + consumer.null(); |
| 124 | + } |
| 125 | + return; |
| 126 | + } |
| 127 | + throw std::logic_error( "invalid value for tao::json::type" ); // LCOV_EXCL_LINE |
| 128 | + } |
| 129 | + |
| 130 | + } // sax |
| 131 | + |
| 132 | + } // json |
| 133 | + |
| 134 | +} // tao |
| 135 | + |
| 136 | +#endif |
0 commit comments