Nullpointers creates null JSON values and vice versa. JSON-92
authorRoker <roker@pep-project.org>
Thu, 03 May 2018 17:21:37 +0200
branchJSON-92
changeset 50194d65bf670d7
parent 500 13d912b6eab8
child 502 d5069e3540f7
Nullpointers creates null JSON values and vice versa.
server/pep-types.cc
     1.1 --- a/server/pep-types.cc	Thu May 03 08:58:45 2018 +0200
     1.2 +++ b/server/pep-types.cc	Thu May 03 17:21:37 2018 +0200
     1.3 @@ -278,6 +278,11 @@
     1.4  template<>
     1.5  char* from_json<char*>(const js::Value& v)
     1.6  {
     1.7 +	if(v.is_null())
     1.8 +	{
     1.9 +		return nullptr;
    1.10 +	}
    1.11 +	
    1.12  	const std::string& ss = v.get_str();
    1.13  	char* s = new_string(ss.c_str(), ss.size() );
    1.14  	return s;
    1.15 @@ -287,10 +292,15 @@
    1.16  template<>
    1.17  message* from_json<message*>(const js::Value& v)
    1.18  {
    1.19 +	if(v.is_null())
    1.20 +	{
    1.21 +		return nullptr;
    1.22 +	}
    1.23 +	
    1.24  	const js::Object& o = v.get_obj();
    1.25 -
    1.26 +	
    1.27  	auto msg = pEp::utility::make_c_ptr(new_message(PEP_dir_incoming), &free_message );
    1.28 -
    1.29 +	
    1.30  	// fetch values from v and put them into msg
    1.31  	msg->dir      = from_json_object<PEP_msg_direction, js::int_type>(o, "dir");
    1.32  	msg->id       = from_json_object<char*, js::str_type>(o, "id");
    1.33 @@ -327,6 +337,11 @@
    1.34  template<>
    1.35  pEp_identity* from_json<pEp_identity*>(const js::Value& v)
    1.36  {
    1.37 +	if(v.is_null())
    1.38 +	{
    1.39 +		return nullptr;
    1.40 +	}
    1.41 +	
    1.42  	const js::Object& o = v.get_obj();
    1.43  	
    1.44  	auto address     = pEp::utility::make_c_ptr(from_json_object<char*, js::str_type>(o, "address")  , &free_string );
    1.45 @@ -365,7 +380,7 @@
    1.46  {
    1.47  	if(msg == nullptr)
    1.48  	{
    1.49 -		return js::Value("NULL-MESSAGE");
    1.50 +		return js::Value();
    1.51  	}
    1.52  	
    1.53  	js::Object o;
    1.54 @@ -409,16 +424,20 @@
    1.55  template<>
    1.56  stringlist_t* from_json<stringlist_t*>(const js::Value& v)
    1.57  {
    1.58 +	if(v.is_null())
    1.59 +	{
    1.60 +		return nullptr;
    1.61 +	}
    1.62 +	
    1.63  	const js::Array& a = v.get_array();
    1.64 -
    1.65  	auto sl = pEp::utility::make_c_ptr(new_stringlist( nullptr ), &free_stringlist );
    1.66 -
    1.67 +	
    1.68  	for(const js::Value& v : a )
    1.69  	{
    1.70  		const std::string& s = v.get_str();
    1.71  		stringlist_add(sl.get(), s.c_str() );
    1.72  	}
    1.73 -
    1.74 +	
    1.75  	return sl.release();
    1.76  }
    1.77  
    1.78 @@ -432,16 +451,20 @@
    1.79  template<>
    1.80  identity_list* from_json<identity_list*>(const js::Value& v)
    1.81  {
    1.82 +	if(v.is_null())
    1.83 +	{
    1.84 +		return nullptr;
    1.85 +	}
    1.86 +	
    1.87  	const js::Array& a = v.get_array();
    1.88 -
    1.89  	auto il = pEp::utility::make_c_ptr(new_identity_list( nullptr ), &free_identity_list);
    1.90 -
    1.91 +	
    1.92  	for(const js::Value& v : a )
    1.93  	{
    1.94  		pEp_identity* id = from_json<pEp_identity*>(v);
    1.95  		identity_list_add(il.get(), id );
    1.96  	}
    1.97 -
    1.98 +	
    1.99  	return il.release();
   1.100  }
   1.101  
   1.102 @@ -449,6 +472,11 @@
   1.103  template<>
   1.104  _bloblist_t* from_json<_bloblist_t*>(const js::Value& v)
   1.105  {
   1.106 +	if(v.is_null())
   1.107 +	{
   1.108 +		return nullptr;
   1.109 +	}
   1.110 +	
   1.111  	const js::Array& a = v.get_array();
   1.112  	if(a.empty())
   1.113  		return nullptr;
   1.114 @@ -519,6 +547,11 @@
   1.115  template<>
   1.116  stringpair_t* from_json<stringpair_t*>(const js::Value& v)
   1.117  {
   1.118 +	if(v.is_null())
   1.119 +	{
   1.120 +		return nullptr;
   1.121 +	}
   1.122 +	
   1.123  	const js::Object& o = v.get_obj();
   1.124  	char* key = from_json_object<char*, js::str_type>(o, "key");
   1.125  	char* val = from_json_object<char*, js::str_type>(o, "value");
   1.126 @@ -533,6 +566,11 @@
   1.127  template<>
   1.128  stringpair_list_t* from_json<stringpair_list_t*>(const js::Value& v)
   1.129  {
   1.130 +	if(v.is_null())
   1.131 +	{
   1.132 +		return nullptr;
   1.133 +	}
   1.134 +	
   1.135  	const js::Array& a = v.get_array();
   1.136  	if(a.empty())
   1.137  		return nullptr;
   1.138 @@ -616,7 +654,7 @@
   1.139  {
   1.140  	if(id == nullptr)
   1.141  	{
   1.142 -		return js::Value("NULL-IDENTITY");
   1.143 +		return js::Value();
   1.144  	}
   1.145  
   1.146  	js::Object o;