/// <inheritdoc/>
            public Variant Decode(VariantValue value, BuiltInType builtinType)
            {
                if (VariantValueEx.IsNull(value))
                {
                    return(Variant.Null);
                }

                //
                // Sanitize json input from user
                //
                value = Sanitize(value, builtinType == BuiltInType.String);

                VariantValue json;

                if (builtinType == BuiltInType.Null ||
                    (builtinType == BuiltInType.Variant &&
                     value.IsObject))
                {
                    //
                    // Let the decoder try and decode the json variant.
                    //
                    json = Serializer.FromObject(new { value });
                }
                else
                {
                    //
                    // Give decoder a hint as to the type to use to decode.
                    //
                    json = Serializer.FromObject(new {
                        value = new {
                            Body = value,
                            Type = (byte)builtinType
                        }
                    });
                }

                //
                // Decode json to a real variant
                //
                using (var text = new StringReader(Serializer.SerializeToString(json)))
                    using (var reader = new Newtonsoft.Json.JsonTextReader(text))
                        using (var decoder = new JsonDecoderEx(reader, Context)) {
                            return(decoder.ReadVariant(nameof(value)));
                        }
            }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public Variant Decode(JToken value, BuiltInType builtinType,
                              ServiceMessageContext context)
        {
            //
            // Sanitize json input from user
            //
            value = Sanitize(value, builtinType == BuiltInType.String);

            JObject json;

            if (builtinType == BuiltInType.Null ||
                (builtinType == BuiltInType.Variant && value is JObject))
            {
                //
                // Let the decoder try and decode the json variant.
                //
                json = new JObject {
                    { nameof(value), value }
                };
            }
            else
            {
                //
                // Give decoder a hint as to the type to use to decode.
                //
                json = new JObject {
                    { nameof(value), new JObject {
                          { "Body", value },
                          { "Type", (byte)builtinType }
                      } }
                };
            }

            //
            // Decode json to a real variant
            //
            using (var decoder = new JsonDecoderEx(json, context ?? Context)) {
                return(decoder.ReadVariant(nameof(value)));
            }
        }