[ios, macos] Convert NSDictionary to GeoJSON

This commit is contained in:
Minh Nguyễn
2020-03-11 18:45:45 -07:00
committed by Minh Nguyễn
parent 93f5290c0e
commit 5196e37d48

View File

@@ -1,3 +1,5 @@
#import "MGLShape_Private.h"
#include <mbgl/style/conversion_impl.hpp>
NS_ASSUME_NONNULL_BEGIN
@@ -124,9 +126,21 @@ public:
}
}
static optional<GeoJSON> toGeoJSON(const Holder&, Error& error) {
error = { "toGeoJSON not implemented" };
return {};
static optional<GeoJSON> toGeoJSON(const Holder& holder, Error& error) {
id object = holder.value;
if ([object isKindOfClass:[NSDictionary class]]) {
NSError *serializationError;
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&serializationError];
if (serializationError) {
error = { std::string(serializationError.localizedDescription.UTF8String) };
return {};
}
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return mapbox::geojson::parse(string.UTF8String);
} else {
error = { std::string([NSString stringWithFormat:@"%@ is not a GeoJSON object.", object].UTF8String) };
return {};
}
}
private: