Skip to content

Commit 7180f68

Browse files
committed
Merge branch 'master' of github.com:5cript/SimpleJSON
2 parents 646afed + 1c4fae7 commit 7180f68

File tree

7 files changed

+136
-22
lines changed

7 files changed

+136
-22
lines changed

parse/jsd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "jsd_string.h"
66
#include "jsd_container.h"
77
#include "jsd_map.h"
8+
#include "jsd_unordered_map.h"
89
#include "jsd_array.h"
910
#include "jsd_set.h"
1011
#include "jsd_pair.h"

parse/jsd_map.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef JSD_MAP_H_INCLUDED
22
#define JSD_MAP_H_INCLUDED
33

4-
#include "jsd_core.h"
4+
#include "jsd_core.h"
5+
#include <map>
56

67
namespace JSON
78
{

parse/jsd_unordered_map.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef JSD_UNORDERED_MAP_H_INCLUDED
2+
#define JSD_UNORDERED_MAP_H_INCLUDED
3+
4+
#include "jsd_core.h"
5+
#include <unordered_map>
6+
7+
namespace JSON
8+
{
9+
template <typename ValueT, typename HashT = std::hash <std::string>, class PredT = std::equal_to <std::string>,
10+
typename AllocT = std::allocator <std::pair <const std::string, ValueT>>>
11+
void parse(std::unordered_map<std::string, ValueT, HashT, PredT, AllocT>& value, std::string const& name,
12+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
13+
{
14+
try
15+
{
16+
GET_CHILD(name, pt, (std::unordered_map<std::string, ValueT>()));
17+
for (auto const& i : pt)
18+
{
19+
ValueT temp;
20+
parse(temp, "", i.second, options);
21+
value[i.first] = temp;
22+
}
23+
}
24+
catch (boost::property_tree::ptree_bad_data& exc)
25+
{
26+
DEFAULT_PROPERTY_ERROR_HANDLER((std::unordered_map<std::string, ValueT>()), (std::unordered_map<std::string, ValueT>()));
27+
}
28+
catch (boost::property_tree::ptree_bad_path& exc)
29+
{
30+
DEFAULT_PATH_ERROR_HANDLER((std::unordered_map<std::string, ValueT>()), (std::unordered_map<std::string, ValueT>()));
31+
}
32+
}
33+
}
34+
35+
#endif // JSD_UNORDERED_MAP_H_INCLUDED

stringify/jss.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "jss_pair.h"
2424
#include "jss_valarray.h"
2525
#include "jss_map.h"
26+
#include "jss_unordered_map.h"
2627
#include "jss_set.h"
2728
#include "jss_atomic.h"
2829
#include "jss_smart_ptr.h"

stringify/jss_map.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,15 @@
22
#define JSS_MAP_H_INCLUDED
33

44
#include "jss_core.h"
5-
#include "jss_optional.h"
5+
#include "jss_optional.h"
6+
#include "map_commons.h"
67
#include <map>
78

89
namespace JSON
910
{
10-
namespace Internal
11-
{
12-
template <typename T,
13-
typename = typename std::enable_if <Internal::can_stringify<T>::value>::type >
14-
void stringify_map_pair (std::ostream& stream, std::pair <const std::string, T> const& value, StringificationOptions const& options)
15-
{
16-
stringify(stream, value.first, value.second, options);
17-
}
18-
19-
template <typename KeyT, typename ValueT,
20-
typename = typename std::enable_if <Internal::can_stringify<ValueT>::value && Internal::can_stringify<KeyT>::value>::type >
21-
void stringify_map_pair_2 (std::ostream& stream, std::pair <const KeyT, ValueT> const& value, StringificationOptions const& options)
22-
{
23-
stringify(stream, {}, value.first, options);
24-
stream << options.delimiter;
25-
stringify(stream, {}, value.second, options);
26-
}
27-
}
28-
2911
template <typename ValueT, typename CompareT = std::less <ValueT>, class AllocT = std::allocator <ValueT>,
3012
typename = typename std::enable_if <Internal::can_stringify<ValueT>::value>::type >
31-
std::ostream& stringify (std::ostream& stream, std::string const& name, std::map<std::string, ValueT, CompareT, AllocT> const& values, StringificationOptions options)
13+
std::ostream& stringify(std::ostream& stream, std::string const& name, std::map<std::string, ValueT, CompareT, AllocT> const& values, StringificationOptions options)
3214
{
3315
using namespace Internal;
3416

stringify/jss_unordered_map.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef JSS_UNORDERED_MAP_H_INCLUDED
2+
#define JSS_UNORDERED_MAP_H_INCLUDED
3+
4+
#include "jss_core.h"
5+
#include "jss_optional.h"
6+
#include "map_commons.h"
7+
#include <unordered_map>
8+
9+
namespace JSON
10+
{
11+
template <typename ValueT, typename HashT = std::hash <std::string>, class PredT = std::equal_to <std::string>,
12+
typename AllocT = std::allocator <std::pair <const std::string, ValueT>>,
13+
typename = typename std::enable_if <Internal::can_stringify<ValueT>::value>::type >
14+
std::ostream& stringify(std::ostream& stream, std::string const& name, std::unordered_map<std::string, ValueT, HashT, PredT, AllocT> const& values, StringificationOptions options)
15+
{
16+
using namespace Internal;
17+
18+
WRITE_OBJECT_START(stream);
19+
options.in_object = true;
20+
21+
bool first = true;
22+
for (auto const& i : values)
23+
{
24+
if (is_optional_set(i.second))
25+
{
26+
if (!first)
27+
stream << options.delimiter;
28+
stringify_map_pair(stream, i, options);
29+
first = false;
30+
}
31+
}
32+
WRITE_OBJECT_END(stream);
33+
return stream;
34+
}
35+
36+
template <typename KeyT,
37+
typename ValueT, typename HashT = std::hash <std::string>, class PredT = std::equal_to <std::string>,
38+
typename AllocT = std::allocator <std::pair <const std::string, ValueT>>,
39+
typename = typename std::enable_if <Internal::can_stringify<ValueT>::value>::type >
40+
std::ostream& stringify(std::ostream& stream, std::string const& name, std::unordered_multimap<KeyT, ValueT, HashT, PredT, AllocT> const& values, StringificationOptions options)
41+
{
42+
using namespace Internal;
43+
44+
WRITE_ARRAY_START(stream);
45+
options.ignore_name = true;
46+
47+
bool first = true;
48+
for (auto const& i : values)
49+
{
50+
if (is_optional_set(i.second))
51+
{
52+
if (!first)
53+
stream << options.delimiter;
54+
WRITE_ARRAY_START(stream);
55+
stringify_map_pair_2(stream, i, options);
56+
WRITE_ARRAY_END(stream);
57+
first = false;
58+
}
59+
}
60+
WRITE_ARRAY_END(stream);
61+
return stream;
62+
}
63+
}
64+
65+
#endif // JSS_UNORDERED_MAP_H_INCLUDED

stringify/map_commons.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef MAP_COMMONS_H_INCLUDED
2+
#define MAP_COMMONS_H_INCLUDED
3+
4+
#include "jss_core.h"
5+
#include <utility>
6+
7+
namespace JSON
8+
{
9+
namespace Internal
10+
{
11+
template <typename T,
12+
typename = typename std::enable_if <Internal::can_stringify<T>::value>::type >
13+
void stringify_map_pair (std::ostream& stream, std::pair <const std::string, T> const& value, StringificationOptions const& options)
14+
{
15+
stringify(stream, value.first, value.second, options);
16+
}
17+
18+
template <typename KeyT, typename ValueT,
19+
typename = typename std::enable_if <Internal::can_stringify<ValueT>::value && Internal::can_stringify<KeyT>::value>::type >
20+
void stringify_map_pair_2 (std::ostream& stream, std::pair <const KeyT, ValueT> const& value, StringificationOptions const& options)
21+
{
22+
stringify(stream, {}, value.first, options);
23+
stream << options.delimiter;
24+
stringify(stream, {}, value.second, options);
25+
}
26+
}
27+
}
28+
29+
#endif // MAP_COMMONS_H_INCLUDED

0 commit comments

Comments
 (0)