Closed nagarajanpn closed 2 years ago
An LDValue makes use of the Encodable protocol. It can be encoded like:
let result = try JSONEncoder().encode(variationResult)
If you want to include that LDValue in another struct, that struct can use the Encodable protocol as well and the LDValue should serialize without issue.
struct ExampleStruct: Encodable {
public var name: String
public var value: LDValue
}
let example = ExampleStruct.init(name: "Example", value: variationResult)
let jsonString = try JSONEncoder().encode(example)
I'm not sure what you mean by this. Could you please explain in more detail?
The LDValue is an Enumeration with an associated value. To unpack the values within the LDValue, you need to do some sort of pattern matching. For example, if you want to convert your result into a dictionary, you can do:
guard case .object(let dictionary) = variationResult
else { return }
print(dictionary["iOS"])
- How to do JSONSerialization of the Above Object , that Object contains LDValue.
An LDValue makes use of the Encodable protocol. It can be encoded like:
let result = try JSONEncoder().encode(variationResult)
@keelerm84 Is this possible in Objective-C? If not, is there any alternative in Objective-C beside iterating through arrays and dictionaries to convert each element/value from an LDValue to a corresponding type that is serializable?
Thanks for your mail
On Sat, 10 Sep, 2022, 12:32 am Joel Skrepnek, @.***> wrote:
- How to do JSONSerialization of the Above Object , that Object contains LDValue.
An LDValue makes use of the Encodable https://developer.apple.com/documentation/swift/encodable protocol. It can be encoded like:
let result = try JSONEncoder().encode(variationResult)
@keelerm84 https://github.com/keelerm84 Is this possible in Objective-C? If not, is there any alternative in Objective-C beside iterating through arrays and dictionaries to convert each element/value from an LDValue to a corresponding type that is serializable?
— Reply to this email directly, view it on GitHub https://github.com/launchdarkly/ios-client-sdk/issues/278#issuecomment-1242358160, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHLIO7AAN7MMXMFDZTGOP7TV5OCUNANCNFSM6AAAAAAQDEZ754 . You are receiving this because you authored the thread.Message ID: @.***>
@keelerm84 Is this possible in Objective-C? If not, is there any alternative in Objective-C beside iterating through arrays and dictionaries to convert each element/value from an LDValue to a corresponding type that is serializable?
No, I do not believe so. Objective-C's NSJSONSerialization seems quite restrictive. For it to work, we would need to add a method on the evaluation result which could represent the entire structure as Foundation objects. This would in turn require us to do the same for LDValue, which requires traversing that entire structure.
Pod Version : ### pod 'LaunchDarkly', '~> 6.1' Code : LDClient.get()?.jsonVariation(forKey: "MyMobileKey", defaultValue:nil) Response : _Optional
▿ some : LDValue
▿ object : 3 elements
▿ 0 : 2 elements
Kindly provide sample code.