JSON Voorhees
Killer JSON for C++
coerce.hpp
Go to the documentation of this file.
1 /** \file jsonv/coerce.hpp
2  * A \c jsonv::value has a number of \c as_X operators, which strictly performs a transformation to a C++ data type.
3  * However, sometimes when working with things like user input, you would like to be more free-form in what you accept
4  * as "valid."
5  *
6  * Copyright (c) 2014-2015 by Travis Gockel. All rights reserved.
7  *
8  * This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
9  * as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
10  * version.
11  *
12  * \author Travis Gockel (travis@gockelhut.com)
13 **/
14 #ifndef __JSONV_COERCE_HPP_INCLUDED__
15 #define __JSONV_COERCE_HPP_INCLUDED__
16 
17 #include <jsonv/config.hpp>
18 #include <jsonv/value.hpp>
19 
20 #include <map>
21 #include <vector>
22 
23 namespace jsonv
24 {
25 
26 /** \addtogroup Coercion
27  * \{
28  * Looser conversion functions to behave more like ECMAScript.
29 **/
30 
31 /** Can the given \c kind be converted \a from a kind \a to another?
32  *
33  * \returns \c true if the corresponding \c coerce_X function for the specified \a to will successfully return if given
34  * a \c value of the kind \a from; false if there is no such conversion (the \c coerce_X function might
35  * throw).
36 **/
37 JSONV_PUBLIC bool can_coerce(const kind& from, const kind& to);
38 
39 /** Can the given \c value be converted \a from a kind \a to another?
40  *
41  * \note
42  * This is \e not only a convenience function! There is a special case for converting from a \c string into either a
43  * \c decimal or \c integer where the contents of the string must be considered. This function will look into the given
44  * \a from and see if it can successfully perfrom the coercion.
45  *
46  * \returns \c true if the corresponding \c coerce_X function for the specified \a to will successfully return if given
47  * the \c value \a from; false if there is no such conversion (the \c coerce_X function will throw).
48 **/
49 JSONV_PUBLIC bool can_coerce(const value& from, const kind& to);
50 
51 /** Coerce \a from into a \c null. If \a from is not \c null, this will throw. It is not clear that there is a use for
52  * this beyond completeness.
53  *
54  * \returns \c nullptr if \a from has \c kind::null.
55  * \throws kind_error if \a from is not \c kind::null.
56 **/
57 JSONV_PUBLIC std::nullptr_t coerce_null(const value& from);
58 
59 /** Coerce \a from into a \c map.
60  *
61  * \returns a map of the contents of \a from.
62  * \throws kind_error if \a from is not \c kind::object.
63 **/
64 JSONV_PUBLIC std::map<std::string, value> coerce_object(const value& from);
65 
66 /** Coerce \a from into a \c vector.
67  *
68  * \returns a vector of the contents of \a from.
69  * \throws kind_error if \a from is not \c kind::array.
70 **/
71 JSONV_PUBLIC std::vector<value> coerce_array(const value& from);
72 
73 /** Coerce \a from into an \c std::string. If \a from is already \c kind::string, the value is simply returned. If
74  * \a from is any other \c kind, the result will be the same as \c to_string.
75 **/
76 JSONV_PUBLIC std::string coerce_string(const value& from);
77 
78 /** Coerce \a from into an integer. If \a from is a \c decimal lower than the minimum of \c std::int64_t or higher than
79  * the maximum of \c std::int64_t, it is clamped to the lowest or highest value, respectively.
80  *
81  * \returns
82  * \c kind is... | Rules
83  * ------------- | -------------------------------------------------
84  * \c null | throws \c kind_error
85  * \c object | throws \c kind_error
86  * \c array | throws \c kind_error
87  * \c string | \c parse(from.as_string()).as_integer()
88  * \c integer | \c from.as_integer()
89  * \c decimal | \c std::int64_t(from.as_decimal())
90  * \c boolean | \c from.as_boolean() ? 1 : 0
91 **/
92 JSONV_PUBLIC std::int64_t coerce_integer(const value& from);
93 
94 /** Coerce \a from into a \c double.
95  *
96  * \returns
97  * \c kind is... | Rules
98  * ------------- | -------------------------------------------------
99  * \c null | throws \c kind_error
100  * \c object | throws \c kind_error
101  * \c array | throws \c kind_error
102  * \c string | \c parse(from.as_string()).as_decimal()
103  * \c integer | \c from.as_decimal()
104  * \c decimal | \c from.as_decimal()
105  * \c boolean | \c from.as_boolean() ? 1.0 : 0.0
106 **/
107 JSONV_PUBLIC double coerce_decimal(const value& from);
108 
109 /** Coerce \a from into a \c bool. This follows the rules of Python's boolean coercion.
110  *
111  * \returns
112  * \c kind is... | Rules
113  * ------------- | --------------------------------------------------
114  * \c null | \c false
115  * \c object | \c !from.empty()
116  * \c array | \c !from.empty()
117  * \c string | \c !from.empty() (even if the value is \c "false")
118  * \c integer | \c from != 0
119  * \c decimal | \c from != 0.0
120  * \c boolean | \c from.as_boolean()
121 **/
122 JSONV_PUBLIC bool coerce_boolean(const value& from);
123 
124 /** Combines \a a and \a b in any way possible. The result kind is \e usually based on the kind of \a a and loosely
125  * follows what ECMAScript does when you call \c + on two values (sort of). If you are looking for "predictable", this
126  * is not the function for you. If you are looking for convenience, this is it.
127 **/
128 JSONV_PUBLIC value coerce_merge(value a, value b);
129 
130 /** \} **/
131 
132 }
133 
134 #endif/*__JSONV_COERCE_HPP_INCLUDED__*/
JSONV_PUBLIC bool can_coerce(const value &from, const kind &to)
Can the given value be converted from a kind to another?
JSONV_PUBLIC std::int64_t coerce_integer(const value &from)
Coerce from into an integer.
JSONV_PUBLIC std::string coerce_string(const value &from)
Coerce from into an std::string.
JSONV_PUBLIC std::vector< value > coerce_array(const value &from)
Coerce from into a vector.
JSONV_PUBLIC value coerce_merge(value a, value b)
Combines a and b in any way possible.
Copyright (c) 2014-2019 by Travis Gockel.
kind
Describes the kind of data a value holds.
Definition: value.hpp:69
JSONV_PUBLIC std::map< std::string, value > coerce_object(const value &from)
Coerce from into a map.
JSONV_PUBLIC bool coerce_boolean(const value &from)
Coerce from into a bool.
JSONV_PUBLIC double coerce_decimal(const value &from)
Coerce from into a double.
#define JSONV_PUBLIC
This function or class is part of the public API for JsonVoorhees.
Definition: config.hpp:104
JSONV_PUBLIC std::nullptr_t coerce_null(const value &from)
Coerce from into a null.
Copyright (c) 2012-2018 by Travis Gockel.