Convert nlohmann::json to NSDictionary
Summary: T46426056: Add utility to convert nlohmann::json to NSDictionary. Use same in instagram logging module. Differential Revision: D16798341 fbshipit-source-id: 88923fcbcd1ffbed5a468cee4ba2f9482ba81a97
This commit is contained in:
committed by
Facebook Github Bot
parent
6a0da24eb2
commit
d68dac2ce0
13
iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h
Normal file
13
iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
#pragma once
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace facebook {
|
||||
namespace cxxutils {
|
||||
using json = nlohmann::json;
|
||||
NSDictionary<NSString *, id> *convertNlohmannJSONToNSDictionary(const json &input);
|
||||
}
|
||||
}
|
||||
71
iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm
Normal file
71
iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "FBCxxNlohmannJSONConvert.h"
|
||||
|
||||
#import <objc/runtime.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace cxxutils {
|
||||
using json = nlohmann::json;
|
||||
|
||||
id convertJSONValue(const json &input);
|
||||
NSString *convertToNSString(const std::string &input);
|
||||
|
||||
id convertJSONValue(const json &input)
|
||||
|
||||
{
|
||||
// for reference see
|
||||
// [github](https://github.com/nlohmann/json/blob/8d6c033f80461123cbfba5e7a3027a9c35ea2eef/include/nlohmann/detail/value_t.hpp#L41)
|
||||
using value_t = json::value_t;
|
||||
|
||||
switch (input.type()) {
|
||||
case value_t::string: {
|
||||
return convertToNSString(input.get<std::string>());
|
||||
}
|
||||
case value_t::object:
|
||||
return convertNlohmannJSONToNSDictionary(input);
|
||||
case value_t::array: {
|
||||
NSMutableArray<id> *array = [[NSMutableArray alloc] initWithCapacity:input.size()];
|
||||
for (auto &elem : input) {
|
||||
id obj = convertJSONValue(elem);
|
||||
if (obj) {
|
||||
[array addObject:obj];
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
case value_t::boolean:
|
||||
return input.get<bool>() ? @YES : @NO;
|
||||
case value_t::number_integer:
|
||||
return @(input.get<int>());
|
||||
case value_t::number_unsigned:
|
||||
return @(input.get<uint>());
|
||||
case value_t::number_float:
|
||||
return @(input.get<float>());
|
||||
case value_t::null:
|
||||
case value_t::discarded:
|
||||
return [NSNull null];
|
||||
}
|
||||
}
|
||||
|
||||
NSString *convertToNSString(const std::string &str)
|
||||
{
|
||||
return [[NSString alloc] initWithBytes:str.c_str() length:str.size() encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
NSDictionary<NSString *, id> *convertNlohmannJSONToNSDictionary(const json &input)
|
||||
{
|
||||
NSMutableDictionary<NSString *, id> *result = [NSMutableDictionary new];
|
||||
|
||||
for (auto &el : input.items()) {
|
||||
const auto key = convertToNSString(el.key());
|
||||
id value = convertJSONValue(el.value());
|
||||
if (value) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return [result copy];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user