This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigPojo.cs
More file actions
48 lines (45 loc) · 1.62 KB
/
ConfigPojo.cs
File metadata and controls
48 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace PlayerDataBackupTool_CSharp
{
public class ConfigPojo
{
public string mongodb_uri;
public string mongodb_database;
public string mongodb_collection;
public string world_playerdata_path;
public string uuid2name_path;
public void saveConfig()
{
File.WriteAllText(@"config.json", ObjectToJson(this));
}
/// <summary>
/// 内存对象转换为json字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToJson(object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream, obj);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
return Encoding.UTF8.GetString(dataBytes);
}
/// <summary>
/// Json字符串转内存对象
/// </summary>
/// <param name="jsonString"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static object JsonToObject(string jsonString, object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
return serializer.ReadObject(mStream);
}
}
}