We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hello,
I tried to encode a structure that has a attribute but it seems it doesn't work. It encode elements but no attributes.
How can I make this work ?
public struct Coordinate { /// attribute : datum public let datum: String = "test123" /// Latitude public let latitude: Double? /// Latitude public let longitude: Double? public init(latitude: Double?, longitude: Double?) { self.latitude = latitude self.longitude = longitude } } // MARK: codable implementation extension Coordinate: Codable, DynamicNodeEncoding, DynamicNodeDecoding { enum CoordinateCodingKeys: String, CodingKey { case datum case latitude = "Lat" case longitude = "Lon" } public static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding { switch key { case CoordinateCodingKeys.latitude: return .element case CoordinateCodingKeys.longitude: return .element default: return .attribute } } public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding { switch key { case CoordinateCodingKeys.latitude: return .element case CoordinateCodingKeys.longitude: return .element default: return .attribute } } }
when I call this :
let xmlEncoder = XMLEncoder() xmlEncoder.keyEncodingStrategy = XMLEncoder.KeyEncodingStrategy.capitalized xmlEncoder.outputFormatting = [.prettyPrinted] let coordinate = Coordinate(latitude: 38.5, longitude: 77.2) let data = try xmlEncoder.encode(coordinate, withRootKey: "Coordinate") let xmlStr = String(data: data!, encoding: .utf8)
The content of xmlStr is :
<Coordinate> <Lat>38.5</Lat> <Lon>77.2</Lon> </Coordinate>
I don't understand why the attribute datum is not set !
Thanks for help.
The text was updated successfully, but these errors were encountered:
I managed to make it work, but find out a bug :)
when using option : xmlEncoder.keyEncodingStrategy = XMLEncoder.KeyEncodingStrategy.capitalized
the attribute is capitalized like an element.
<Coordinate Datum="wgs84"> <Lat>38.5</Lat> <Lon>77.2</Lon> </Coordinate>
Sorry, something went wrong.
when using option : xmlEncoder.keyEncodingStrategy = XMLEncoder.KeyEncodingStrategy.capitalized the attribute is capitalized like an element.
Thanks for reporting. This is the intended behavior. "Keys" means both elements and attributes, depending on context.
No branches or pull requests
Hello,
I tried to encode a structure that has a attribute but it seems it doesn't work. It encode elements but no attributes.
How can I make this work ?
when I call this :
The content of xmlStr is :
I don't understand why the attribute datum is not set !
Thanks for help.
The text was updated successfully, but these errors were encountered: