From d68dac2ce0179e7f6c26603390233902356537d9 Mon Sep 17 00:00:00 2001 From: Spencer Baumgardner Date: Wed, 14 Aug 2019 17:24:15 -0700 Subject: [PATCH] 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 --- .../FBCxxUtils/FBCxxNlohmannJSONConvert.h | 13 ++++ .../FBCxxUtils/FBCxxNlohmannJSONConvert.mm | 71 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h create mode 100644 iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm diff --git a/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h b/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h new file mode 100644 index 000000000..234f686c1 --- /dev/null +++ b/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.h @@ -0,0 +1,13 @@ +// Copyright 2004-present Facebook. All Rights Reserved. +#pragma once + +#import + +#include + +namespace facebook { +namespace cxxutils { +using json = nlohmann::json; +NSDictionary *convertNlohmannJSONToNSDictionary(const json &input); +} +} diff --git a/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm b/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm new file mode 100644 index 000000000..9a9cdf852 --- /dev/null +++ b/iOS/FlipperKit/FBCxxUtils/FBCxxNlohmannJSONConvert.mm @@ -0,0 +1,71 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#import "FBCxxNlohmannJSONConvert.h" + +#import + +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()); + } + case value_t::object: + return convertNlohmannJSONToNSDictionary(input); + case value_t::array: { + NSMutableArray *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() ? @YES : @NO; + case value_t::number_integer: + return @(input.get()); + case value_t::number_unsigned: + return @(input.get()); + case value_t::number_float: + return @(input.get()); + 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 *convertNlohmannJSONToNSDictionary(const json &input) +{ + NSMutableDictionary *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]; +} +} +}