From 802f67667a50db0be2cd42a4448506b2825e236e Mon Sep 17 00:00:00 2001 From: Chris Eplett Date: Fri, 3 Nov 2023 09:22:58 -0400 Subject: [PATCH] Udpate the README Add more detail around new Codable support --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0ee5fdf..a934a50 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,20 @@ let dict = [ // [{major 3, minor 0, patch 0,... ] // SemanticVersion is Codable -let data = try JSONEncoder().encode(v123) // 58 bytes -let decoded = try JSONDecoder().decode(SemanticVersion.self, from: data) // 1.2.3 -decoded == v123 // true +// Note: the strategy defaults to `.semverString` +let stringEncoder = JSONEncoder() +stringEncoder.semanticVersionEncodingStrategy = .semverString +let stringDecoder = JSONDecoder() +stringDecoder.semanticVersionDecodingStrategy = .semverString +let stringData = try stringEncoder.encode(v123) // 7 bytes -> "1.2.3", including quotes +let stringDecoded = try stringDecoder.decode(SemanticVersion.self, from: stringData) // 1.2.3 +stringDecoded == v123 // true + +let memberwiseEncoder = JSONEncoder() +memberwiseEncoder.semanticVersionEncodingStrategy = .memberwise +let memberwiseDecoder = JSONDecoder() +memberwiseDecoder.semanticVersionDecodingStrategy = .memberwise +let memberwiseData = try memberwiseEncoder.encode(v123) // 58 bytes +let memberwiseDecoded = try memberwiseDecoder.decode(SemanticVersion.self, from: memberwiseData) // 1.2.3 +memberwiseDecoded == v123 // true ```