public void ConfigMapV1_Data_PreserveKeyCase(string key) { var model = new ConfigMapV1 { Data = { [key] = key } }; JObject rootObject; using (JTokenWriter writer = new JTokenWriter()) { JsonSerializer.Create(KubeResourceClient.SerializerSettings).Serialize(writer, model); writer.Flush(); rootObject = (JObject)writer.Token; } Log.LogInformation("Serialized:\n{JSON:l}", rootObject.ToString(Formatting.Indented) ); JObject data = rootObject.Value <JObject>("data"); Assert.NotNull(data); Assert.Equal(key, data.Value <string>(key) ); }
public void KubeObjectV1_Empty_CollectionProperty(Type modelType, string targetPropertyName, bool shouldBeSerialized) { object model = Activator.CreateInstance(modelType); JObject rootObject; using (JTokenWriter writer = new JTokenWriter()) { new JsonSerializer().Serialize(writer, model); writer.Flush(); rootObject = (JObject)writer.Token; } string expectedPropertyName = GetJsonPropertyName(modelType, targetPropertyName); string[] actualPropertyNames = rootObject.Properties().Select(property => property.Name).ToArray(); if (shouldBeSerialized) { Assert.Contains(expectedPropertyName, actualPropertyNames); } else { Assert.DoesNotContain(expectedPropertyName, actualPropertyNames); } }
private void SetKeyValue(string KeyName, object TheValue, RegistryValueKind type) { if (this.readJson()) { JToken token; bool write = true; if (AIDASettings.TryGetValue(KeyName, out token)) { string asdd = (token.Value <object>()).ToString(); if (type == RegistryValueKind.String || type == RegistryValueKind.DWord || type == RegistryValueKind.ExpandString || type == RegistryValueKind.MultiString || type == RegistryValueKind.QWord) { write = (!string.IsNullOrEmpty(asdd) && asdd.ToLower() != TheValue.ToString().ToLower()); } } if (write) { using (JTokenWriter asd = new JTokenWriter()) { asd.WriteValue(TheValue); asd.Flush(); AIDASettings[KeyName] = asd.Token; using (StreamWriter sr = new StreamWriter(AIDASettingsLocation, false)) using (Newtonsoft.Json.JsonTextWriter jr = new Newtonsoft.Json.JsonTextWriter(sr)) { AIDASettings.WriteTo(jr); jr.Flush(); } this.jsonLastWrite = DateTime.Now; File.SetLastWriteTime(AIDASettingsLocation, this.jsonLastWrite); } } } this.theReg.SetValue(KeyName, TheValue, type); this.theReg.Flush(); }
public void Can_Serialize_Int32OrStringV1_Null(object rawValue, object renderedValue) { Int32OrStringV1 int32OrString; if (rawValue is string stringValue) { int32OrString = stringValue; } else if (rawValue is int intValue) { int32OrString = intValue; } else if (rawValue == null) { int32OrString = null; } else { throw new InvalidOperationException($"Unexpected value type: '{rawValue.GetType().FullName}'."); } var model = new TestModel { Number = 123, Text = "hello", Mixed = int32OrString }; JObject rootObject; using (JTokenWriter writer = new JTokenWriter()) { JsonSerializer .Create(new JsonSerializerSettings { Converters = { new Int32OrStringV1Converter() } }) .Serialize(writer, model); writer.Flush(); rootObject = (JObject)writer.Token; } string propertyName = GetJsonPropertyName(typeof(TestModel), "Mixed"); var propertyValue = rootObject.Property(propertyName)?.Value as JValue; Assert.NotNull(propertyValue); Assert.Equal(renderedValue, propertyValue.Value); }
public void KubeObject_JTokenType(Type kubeObjectType, JTokenType expectedTokenType) { KubeObjectV1 kubeObject = (KubeObjectV1)Activator.CreateInstance(kubeObjectType); JToken rootToken; using (JTokenWriter writer = new JTokenWriter()) { new JsonSerializer().Serialize(writer, kubeObject); writer.Flush(); rootToken = writer.Token; } Assert.NotNull(rootToken); Assert.Equal(rootToken.Type, expectedTokenType); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Func <object, JArray> getJObjects = instance => { using (var tWriter = new JTokenWriter()) { serializer.Serialize(tWriter, instance); tWriter.Flush(); return((JArray)tWriter.Token); } }; var request = (AllDocsRequest)value; var keys = getJObjects(request.Keys); var jObject = new JObject(); jObject.Add("keys", keys); jObject.WriteTo(writer); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Func <object, JObject> getJObject = instance => { using (var tWriter = new JTokenWriter()) { serializer.Serialize(tWriter, instance); tWriter.Flush(); return((JObject)tWriter.Token); } }; var request = (BulkDocsRequest)value; writer.WriteStartObject(); //writer.WritePropertyName("all_or_nothing"); //writer.WriteValue(true); writer.WritePropertyName("docs"); writer.WriteStartArray(); foreach (var doc in request.Docs) { //delete if (doc.Delete) { var toRemove = new JObject(); toRemove.Add("_id", new JValue(doc.Id)); toRemove.Add("_rev", new JValue(doc.Rev)); toRemove.Add("_deleted", true); toRemove.WriteTo(writer); continue; } //add or update var jsonDoc = getJObject(doc.Content); jsonDoc.WriteTo(writer); } writer.WriteEndArray(); writer.WriteEndObject(); }