Beispiel #1
0
        public void WriteJson(object value, IValueWriter writer)
        {
            var segment = (Segment)value;
            var obj     = writer.Object();

            obj.Name("key").String(segment.Key);
            obj.Name("version").Int(segment.Version);
            obj.Name("deleted").Bool(segment.Deleted);
            SerializationHelpers.WriteStrings(obj.Name("included"), segment.Included);
            SerializationHelpers.WriteStrings(obj.Name("excluded"), segment.Excluded);
            obj.Name("salt").String(segment.Salt);

            var rulesArr = obj.Name("rules").Array();

            foreach (var r in segment.Rules)
            {
                var ruleObj = rulesArr.Object();
                SerializationHelpers.WriteClauses(ruleObj.Name("clauses"), r.Clauses);
                if (r.Weight.HasValue)
                {
                    ruleObj.Name("weight").Int(r.Weight.Value);
                }
                if (r.BucketBy.HasValue)
                {
                    ruleObj.Name("bucketBy").String(r.BucketBy.Value.AttributeName);
                }
                ruleObj.End();
            }
            rulesArr.End();

            obj.MaybeName("unbounded", segment.Unbounded).Bool(segment.Unbounded);
            obj.MaybeName("generation", segment.Generation.HasValue).IntOrNull(segment.Generation);

            obj.End();
        }
Beispiel #2
0
            public static void WriteJsonValue(EvaluationReason value, IValueWriter writer)
            {
                var obj = writer.Object();

                obj.Name("kind").String(EvaluationReasonKindConverter.ToIdentifier(value.Kind));
                switch (value.Kind)
                {
                case EvaluationReasonKind.RuleMatch:
                    obj.Name("ruleIndex").Int(value.RuleIndex ?? 0);
                    obj.Name("ruleId").String(value.RuleId);
                    break;

                case EvaluationReasonKind.PrerequisiteFailed:
                    obj.Name("prerequisiteKey").String(value.PrerequisiteKey);
                    break;

                case EvaluationReasonKind.Error:
                    obj.Name("errorKind").String(EvaluationErrorKindConverter.ToIdentifier(value.ErrorKind.Value));
                    break;
                }
                if (value.InExperiment)
                {
                    obj.Name("inExperiment").Bool(true); // omit property if false
                }
                if (value.BigSegmentsStatus.HasValue)
                {
                    obj.Name("bigSegmentsStatus").String(
                        BigSegmentsStatusConverter.ToIdentifier(value.BigSegmentsStatus.Value));
                }
                obj.End();
            }
            public void WriteJson(object o, IValueWriter writer)
            {
                var instance = o as MyTestClass;
                var obj      = writer.Object();

                obj.Name("value").String(instance.Value);
                obj.End();
            }
        private void WriteTestStruct(IValueWriter w, TestStruct ts)
        {
            var obj = w.Object();

            obj.Name("str").String(ts.Str);
            WriteInts(obj.Name("nums"), ts.Nums);
            obj.Name("bool").Bool(ts.Bool);
            if (ts.Nested != null)
            {
                WriteTestStruct(obj.Name("nested"), ts.Nested);
            }
            obj.End();
        }
Beispiel #5
0
            public static void WriteJsonValue(LdValue value, IValueWriter writer)
            {
                switch (value.Type)
                {
                case LdValueType.Null:
                    writer.Null();
                    break;

                case LdValueType.Bool:
                    writer.Bool(value.AsBool);
                    break;

                case LdValueType.Number:
                    var asInt    = value.AsInt;
                    var asDouble = value.AsDouble;
                    if ((double)asInt == asDouble)
                    {
                        writer.Int(asInt);
                    }
                    else
                    {
                        writer.Double(asDouble);
                    }
                    break;

                case LdValueType.String:
                    writer.String(value.AsString);
                    break;

                case LdValueType.Array:
                    var arr = writer.Array();
                    foreach (var v in value.List)
                    {
                        WriteJsonValue(v, arr);
                    }
                    arr.End();
                    break;

                case LdValueType.Object:
                    var obj = writer.Object();
                    foreach (var kv in value.Dictionary)
                    {
                        WriteJsonValue(kv.Value, obj.Name(kv.Key));
                    }
                    obj.End();
                    break;
                }
            }
 public static void WriteJsonValue(FeatureFlag value, IValueWriter writer)
 {
     using (var ow = writer.Object())
     {
         LdJsonConverters.LdValueConverter.WriteJsonValue(value.Value, ow.Name("value"));
         ow.Name("version").Int(value.Version);
         ow.MaybeName("flagVersion", value.FlagVersion.HasValue).Int(value.FlagVersion.GetValueOrDefault());
         ow.MaybeName("variation", value.Variation.HasValue).Int(value.Variation.GetValueOrDefault());
         if (value.Reason.HasValue)
         {
             LdJsonConverters.EvaluationReasonConverter.WriteJsonValue(value.Reason.Value, ow.Name("reason"));
         }
         ow.MaybeName("trackEvents", value.TrackEvents).Bool(value.TrackEvents);
         ow.MaybeName("trackReason", value.TrackReason).Bool(value.TrackReason);
         ow.MaybeName("debugEventsUntilDate", value.DebugEventsUntilDate.HasValue)
         .Long(value.DebugEventsUntilDate.GetValueOrDefault().Value);
     }
 }
Beispiel #7
0
            public void WriteJson(object value, IValueWriter writer)
            {
                var user = (User)value;

                if (user is null)
                {
                    writer.Null();
                    return;
                }
                var obj = writer.Object();

                obj.Name("key").String(user.Key);
                obj.MaybeName("secondary", user.Secondary != null).String(user.Secondary);
                obj.MaybeName("ip", user.IPAddress != null).String(user.IPAddress);
                obj.MaybeName("country", user.Country != null).String(user.Country);
                obj.MaybeName("firstName", user.FirstName != null).String(user.FirstName);
                obj.MaybeName("lastName", user.LastName != null).String(user.LastName);
                obj.MaybeName("name", user.Name != null).String(user.Name);
                obj.MaybeName("avatar", user.Avatar != null).String(user.Avatar);
                obj.MaybeName("email", user.Email != null).String(user.Email);
                if (user.AnonymousOptional.HasValue)
                {
                    obj.Name("anonymous").Bool(user.Anonymous);
                }
                if (user.Custom.Count > 0)
                {
                    var customObj = obj.Name("custom").Object();
                    foreach (var kv in user.Custom)
                    {
                        LdValueConverter.WriteJsonValue(kv.Value, customObj.Name(kv.Key));
                    }
                    customObj.End();
                }
                if (user.PrivateAttributeNames.Count > 0)
                {
                    var attrsArr = obj.Name("privateAttributeNames").Array();
                    foreach (var n in user.PrivateAttributeNames)
                    {
                        attrsArr.String(n);
                    }
                    attrsArr.End();
                }
                obj.End();
            }
Beispiel #8
0
            public void WriteJson(object o, IValueWriter writer)
            {
                var instance = (MyTestClass)o;
                var obj      = writer.Object();

                obj.Name("stringProp").String(instance.StringProp);
                obj.Name("boolProp").Bool(instance.BoolProp);
                obj.Name("intProp").Int(instance.IntProp);
                obj.Name("longProp").Long(instance.LongProp);
                obj.Name("doubleProp").Double(instance.DoubleProp);
                if (instance.ArrayOfInts != null)
                {
                    using (var arr = obj.Name("arrayOfInts").Array())
                    {
                        foreach (var n in instance.ArrayOfInts)
                        {
                            arr.Int(n);
                        }
                    }
                }
                if (instance.ObjectOfInts != null)
                {
                    using (var subObj = obj.Name("objectOfInts").Object())
                    {
                        foreach (var kv in instance.ObjectOfInts)
                        {
                            subObj.Name(kv.Key).Int(kv.Value);
                        }
                    }
                }
                obj.Name("nullableString1").String(instance.NullableString1);
                obj.Name("nullableBool1").BoolOrNull(instance.NullableBool1);
                obj.Name("nullableInt1").IntOrNull(instance.NullableInt1);
                obj.Name("nullableLong1").LongOrNull(instance.NullableLong1);
                obj.Name("nullableDouble1").DoubleOrNull(instance.NullableDouble1);
                obj.Name("nullableString2").String(instance.NullableString2);
                obj.Name("nullableBool2").BoolOrNull(instance.NullableBool2);
                obj.Name("nullableInt2").IntOrNull(instance.NullableInt2);
                obj.Name("nullableLong2").LongOrNull(instance.NullableLong2);
                obj.Name("nullableDouble2").DoubleOrNull(instance.NullableDouble2);
                obj.End();
            }
        public void WriteJson(object value, IValueWriter writer)
        {
            var state = value as FeatureFlagsState;

            if (state is null)
            {
                writer.Null();
                return;
            }

            var obj = writer.Object();

            foreach (var entry in state._flags)
            {
                LdValueConverter.WriteJsonValue(entry.Value.Value, obj.Name(entry.Key));
            }

            obj.Name("$valid").Bool(state._valid);

            var allMetadataObj = obj.Name("$flagsState").Object();

            foreach (var entry in state._flags)
            {
                var flagMetadataObj = allMetadataObj.Name(entry.Key).Object();
                var meta            = entry.Value;
                flagMetadataObj.Name("variation").IntOrNull(meta.Variation);
                flagMetadataObj.Name("version").IntOrNull(meta.Version);
                flagMetadataObj.MaybeName("trackEvents", meta.TrackEvents).Bool(meta.TrackEvents);
                flagMetadataObj.MaybeName("debugEventsUntilDate", meta.DebugEventsUntilDate.HasValue)
                .Long(meta.DebugEventsUntilDate?.Value ?? 0);
                if (meta.Reason.HasValue)
                {
                    EvaluationReasonConverter.WriteJsonValue(meta.Reason.Value, flagMetadataObj.Name("reason"));
                }
                flagMetadataObj.End();
            }
            allMetadataObj.End();

            obj.End();
        }
Beispiel #10
0
        public void WriteJson(object value, IValueWriter writer)
        {
            var flag = (FeatureFlag)value;
            var obj  = writer.Object();

            obj.Name("key").String(flag.Key);
            obj.Name("version").Int(flag.Version);
            obj.Name("deleted").Bool(flag.Deleted);
            obj.Name("on").Bool(flag.On);

            var prereqsArr = obj.Name("prerequisites").Array();

            foreach (var p in flag.Prerequisites)
            {
                var prereqObj = prereqsArr.Object();
                prereqObj.Name("key").String(p.Key);
                prereqObj.Name("variation").Int(p.Variation);
                prereqObj.End();
            }
            prereqsArr.End();

            var targetsArr = obj.Name("targets").Array();

            foreach (var t in flag.Targets)
            {
                var targetObj = targetsArr.Object();
                targetObj.Name("variation").Int(t.Variation);
                SerializationHelpers.WriteStrings(targetObj.Name("values"), t.Values);
                targetObj.End();
            }
            targetsArr.End();

            var rulesArr = obj.Name("rules").Array();

            foreach (var r in flag.Rules)
            {
                var ruleObj = rulesArr.Object();
                ruleObj.Name("id").String(r.Id);
                SerializationHelpers.WriteVariationOrRollout(ref ruleObj, r.Variation, r.Rollout);
                SerializationHelpers.WriteClauses(ruleObj.Name("clauses"), r.Clauses);
                ruleObj.Name("trackEvents").Bool(r.TrackEvents);
                ruleObj.End();
            }
            rulesArr.End();

            var fallthroughObj = obj.Name("fallthrough").Object();

            SerializationHelpers.WriteVariationOrRollout(ref fallthroughObj, flag.Fallthrough.Variation, flag.Fallthrough.Rollout);
            fallthroughObj.End();

            if (flag.OffVariation.HasValue)
            {
                obj.Name("offVariation").Int(flag.OffVariation.Value);
            }
            SerializationHelpers.WriteValues(obj.Name("variations"), flag.Variations);
            obj.Name("salt").String(flag.Salt);
            obj.MaybeName("trackEvents", flag.TrackEvents).Bool(flag.TrackEvents);
            obj.MaybeName("trackEventsFallthrough", flag.TrackEventsFallthrough).Bool(flag.TrackEventsFallthrough);
            if (flag.DebugEventsUntilDate.HasValue)
            {
                obj.Name("debugEventsUntilDate").Long(flag.DebugEventsUntilDate.Value.Value);
            }
            obj.Name("clientSide").Bool(flag.ClientSide);

            obj.End();
        }
            public void WriteJson(object instance, IValueWriter writer)
            {
                switch (instance)
                {
                case null:
                    writer.Null();
                    break;

                case bool value:
                    writer.Bool(value);
                    break;

                case int value:
                    writer.Int(value);
                    break;

                case long value:
                    writer.Long(value);
                    break;

                case float value:
                    writer.Double(value);
                    break;

                case double value:
                    writer.Double(value);
                    break;

                case string value:
                    writer.String(value);
                    break;

                case IReadOnlyDictionary <string, object> dict:
                    var obj = writer.Object();
                    foreach (var kv in dict)
                    {
                        WriteJson(kv.Value, obj.Name(kv.Key));
                    }
                    obj.End();
                    break;

                case IReadOnlyDictionary <object, object> dict:
                    var obj1 = writer.Object();
                    foreach (var kv in dict)
                    {
                        WriteJson(kv.Value, obj1.Name(kv.Key.ToString()));
                    }
                    obj1.End();
                    break;

                case IEnumerable <object> list:
                    var arr = writer.Array();
                    foreach (var o in list)
                    {
                        WriteJson(o, arr);
                    }
                    arr.End();
                    break;

                default:
                    throw new ArgumentException(string.Format("ConvertSimpleTypes does not support type {0}",
                                                              instance.GetType()));
                }
            }