launchdarkly / ios-client-sdk

LaunchDarkly Client-side SDK for iOS (Swift and Obj-C)
https://docs.launchdarkly.com/sdk/client-side/ios
Other
70 stars 84 forks source link

Not able to retrieve LDValue from Object. #278

Closed nagarajanpn closed 2 years ago

nagarajanpn commented 2 years ago

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.

keelerm84 commented 2 years ago
  1. 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)

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)
  1. Trying to do decode that json object but , during pass the json data facing error.

I'm not sure what you mean by this. Could you please explain in more detail?

  1. I am trying to convert LDvalue to dictionary or array i could not able to do.

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"])
jskrepnek commented 2 years ago
  1. 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?

nagarajanpn commented 2 years ago

Thanks for your mail

On Sat, 10 Sep, 2022, 12:32 am Joel Skrepnek, @.***> wrote:

  1. 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 commented 2 years ago

@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.