public void TestSerializer() { TempTestClass classA = new TempTestClass { ID = 1, Name = "NNN", Arr = new int[] { 1, 2, 3 } }; string json = SampleJsonSerializaer.Serialize(classA, typeof(TempTestClass)); string json2 = SampleJsonSerializaer.Serialize <TempTestClass>(classA); Assert.True(json == json2);//序列化是否成功 TempTestClass classB = SampleJsonSerializaer.DeSerialize(json2, typeof(TempTestClass)) as TempTestClass; TempTestClass classC = SampleJsonSerializaer.DeSerialize <TempTestClass>(json); Assert.True(Compare(classA, classB) && Compare(classA, classB));//反序列化是否成功 json = json.Replace("NNN", "A"); classB = SampleJsonSerializaer.DeSerialize <TempTestClass>(json); classC = SampleJsonSerializaer.DeSerialize <TempTestClass>(json2); Assert.False(Compare(classB, classC));//修改json的情况下是否正确 }
static ConfigManager() { FileHelper.TryReadAllText("Config.json", out string configJson); if (string.IsNullOrEmpty(configJson))//创建新配置文件 { Config = new Configuration(); Config.Password = RandomString.Next(16); Config.AdditionalConfig.Add("SampleKey", "SampleValue"); SaveConfig(); return; } Config = SampleJsonSerializaer.DeSerialize <Configuration>(configJson); SaveConfig(); }
public bool Login() { string requestBody = string.Format("username={0}&password={1}", _config.JudgerName, _config.Password); try { string result = _webClient.UploadString(loginUrl, requestBody, 3); ServerMessage message = SampleJsonSerializaer.DeSerialize <ServerMessage>(result); return(message.IsSuccessful); } catch { return(false); } }
/// <summary> /// 保存配置信息 /// </summary> public static void SaveConfig() { FileHelper.TryWriteAllText("Config.json", SampleJsonSerializaer.Serialize <Configuration>(Config)); }