forked from beached/daw_json_link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dawjsonlink.cpp
113 lines (99 loc) · 2.67 KB
/
test_dawjsonlink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Darrell Wright
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/beached/daw_json_link
//
#include "defines.h"
#include <daw/json/daw_json_iterator.h>
#include <daw/json/daw_json_link.h>
#include <fstream>
#include <iostream>
#include <libnotify.hpp>
#include <sstream>
#include <string_view>
#include <unistd.h>
struct coordinate_t {
double x;
double y;
double z;
// ignore string name
// ignore object opts
};
struct coordinates_t {
std::vector<coordinate_t> coordinates;
};
namespace daw::json {
template<>
struct json_data_contract<coordinate_t> {
#ifdef DAW_JSON_CNTTP_JSON_NAME
using type =
json_member_list<json_number<"x">, json_number<"y">, json_number<"z">>;
#else
constexpr inline static char const x[] = "x";
constexpr inline static char const y[] = "y";
constexpr inline static char const z[] = "z";
using type =
json_member_list<json_number<x>, json_number<y>, json_number<z>>;
#endif
};
template<>
struct json_data_contract<coordinates_t> {
#ifdef DAW_JSON_CNTTP_JSON_NAME
using type = json_member_list<json_array<"coordinates", coordinate_t>>;
#else
constexpr inline static char const coordinates[] = "coordinates";
using type = json_member_list<json_array<coordinates, coordinate_t>>;
#endif
};
} // namespace daw::json
std::string read_file( std::string const &filename ) {
std::ifstream f( filename );
if( !f ) {
return { };
}
return std::string( std::istreambuf_iterator<char>( f ),
std::istreambuf_iterator<char>( ) );
}
int main( int argc, char *argv[] )
#ifdef DAW_USE_EXCEPTIONS
try
#endif
{
std::string const text = read_file( "/tmp/1.json" );
auto const json_sv = std::string_view( text.data( ), text.size( ) );
double x = 0, y = 0, z = 0;
int len = 0;
{
std::stringstream ostr;
ostr << "C++ DAW JSON Link\t" << getpid( );
notify( ostr.str( ) );
}
using range_t = daw::json::json_array_range<coordinate_t, true>;
auto rng = range_t( json_sv, "coordinates" );
for( auto c : rng ) {
++len;
x += c.x;
y += c.y;
z += c.z;
}
std::cout << x / len << '\n';
std::cout << y / len << '\n';
std::cout << z / len << '\n';
notify( "stop" );
return EXIT_SUCCESS;
}
#ifdef DAW_USE_EXCEPTIONS
catch( daw::json::json_exception const &jex ) {
std::cerr << "Exception thrown by parser: " << jex.reason( ) << '\n';
exit( 1 );
} catch( std::exception const &ex ) {
std::cerr << "Unknown exception thrown during testing: " << ex.what( )
<< '\n';
exit( 1 );
} catch( ... ) {
std::cerr << "Unknown exception thrown during testing\n";
throw;
}
#endif