Created
February 3, 2017 12:14
-
-
Save usagi/414ad5942e5f3ed36021edfdaf4bcb1c to your computer and use it in GitHub Desktop.
picojson::value を bool へ ECMA-262 互換で変換する実装例 ref: http://qiita.com/usagi/items/fe09583b4efe2fe77ead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "type.hxx" | |
namespace usagi::json::picojson | |
{ | |
static inline auto to_bool( const boolean_type in ) { return in; } | |
/// @note ECMA-262 NaN: Boolean( 0/0 ) -> false | |
/// @note ECMA-262 +Inf: Boolean( +1/0 ) -> true | |
/// @note ECMA-262 -Inf: Boolean( -1/0 ) -> true | |
static inline auto to_bool( const number_type in ) | |
{ | |
return not std::isnan( in ) and in != 0; | |
} | |
/// @note ECMA-262 empty-string: Boolean( "" ) -> false | |
static inline auto to_bool( const string_type& in ) | |
{ | |
return not in.empty(); | |
} | |
/// @note: ECMA-262 array: Boolean( [] ) -> true | |
static inline auto to_bool( const array_type& ) | |
{ return true; } | |
/// @note: ECMA-262 object: Boolean( {} ) -> true | |
static inline auto to_bool( const object_type& ) | |
{ return true; } | |
/// @note: ECMA-262 null: Boolean( null ) -> false | |
static inline auto to_bool( const null_type& = null_type() ) | |
{ return false; } | |
/// @brief ECMA-262 Boolean( in ) 互換変換 | |
static inline auto to_bool( const value_type& in ) | |
{ | |
if ( in.is< boolean_type >() ) | |
return to_bool( in.get< boolean_type >() ); | |
if ( in.is< number_type >() ) | |
return to_bool( in.get< number_type >() ); | |
if ( in.is< string_type >() ) | |
return to_bool( in.get< string_type >() ); | |
if ( in.is< array_type >() ) | |
return to_bool( in.get< array_type >() ); | |
if ( in.is< object_type >() ) | |
return to_bool( in.get< object_type >() ); | |
if ( in.is< null_type >() ) | |
return to_bool(); | |
return false; | |
} | |
/// @brief to_bool した結果を value_type で得る syntax sugar | |
static inline auto to_bool_value( const value_type& in ) | |
{ return value_type( to_bool( in ) ); } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "make_value.hxx" | |
#include "to_bool.hxx" | |
#include <iostream> | |
#include <cmath> | |
auto main() -> int | |
{ | |
using namespace std; | |
using namespace usagi::json::picojson; | |
cout | |
<< "null -> " << boolalpha << to_bool( make_value() ) << '\n' | |
<< "0 -> " << boolalpha << to_bool( make_value( 0 ) ) << '\n' | |
<< "1 -> " << boolalpha << to_bool( make_value( 1 ) ) << '\n' | |
<< "-1 -> " << boolalpha << to_bool( make_value( -1 ) ) << '\n' | |
<< "\"\" -> " << boolalpha << to_bool( make_value( "" ) ) << '\n' | |
<< "\"hoge\" -> " << boolalpha << to_bool( make_value( "hoge" ) ) << '\n' | |
<< "false -> " << boolalpha << to_bool( make_value( false ) ) << '\n' | |
<< "true -> " << boolalpha << to_bool( make_value( true ) ) << '\n' | |
<< "[] -> " << boolalpha << to_bool( make_array_value() ) << '\n' | |
<< "{} -> " << boolalpha << to_bool( make_object_value() ) << '\n' | |
; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
null -> false | |
0 -> false | |
1 -> true | |
-1 -> true | |
"" -> false | |
"hoge" -> true | |
false -> false | |
true -> true | |
[] -> true | |
{} -> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment