Exemplo n.º 1
0
        public void Composition_InappropriateData_DoesNotDecode()
        {
            var actual = ImaginationConfigCodec
                         .DecodeString(@"{
                'url': 1,
                'connectionLimit': -1,
                'email': false,
                'advanced': {
                    'allowNonsecure': false,
                    'superAdvanced': { 'childField': true }
                }
            }")
                         .Match(
                v => DecoderErrors.Empty,
                e => e
                );

            var expected =
                DecoderErrors.Single("url", "Expected a string value")
                .Append(DecoderErrors.Single("connectionLimit", "Value must be positive"))
                .Append(DecoderErrors.Single("trustedUrls", "Value is required"))
                .Append(DecoderErrors.Single("email", "Expected a string value"))
                .Append(DecoderErrors.Single("advanced.superAdvanced.childField", "Expected a string value"));

            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decode a JSON string.
        /// </summary>
        public static Either <DecoderErrors, T> DecodeString <T>(
            this Codec <JToken, T> codec,
            [NotNull] string json)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            try
            {
                var jToken = JToken.Parse(json);
                return(codec.Decode(jToken));
            }
            catch (JsonException e)
            {
                return(DecoderErrors.Single("", $"Syntax error: {e.Message}"));
            }
        }