-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_dawjsonlink.cpp
More file actions
108 lines (95 loc) · 3.13 KB
/
test_dawjsonlink.cpp
File metadata and controls
108 lines (95 loc) · 3.13 KB
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
// The MIT License (MIT)
//
// Copyright (c) 2020 Darrell Wright
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files( the "Software" ), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#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 __cpp_nontype_template_parameter_class
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 __cpp_nontype_template_parameter_class
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[] ) {
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;
}