Serializer.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. namespace Assets.HeroEditor4D.Common.Scripts.Common
  5. {
  6. /// <summary>
  7. /// This is a replacement for Newtonsoft serializer to avoid the DLL use. You can use Newtonsoft if you want.
  8. /// </summary>
  9. internal static class Serializer
  10. {
  11. internal static string Serialize(Dictionary<string, string> dict)
  12. {
  13. return "{" + string.Join(",", dict.Select(i => $"\"{i.Key}\":\"{i.Value}\"")) + "}";
  14. }
  15. internal static Dictionary<string, string> DeserializeDict(string json)
  16. {
  17. var dict = new Dictionary<string, string>();
  18. foreach (Match match in Regex.Matches(json, "\"(.*?)\":(?:\"(.*?)\")?"))
  19. {
  20. dict.Add(match.Groups[1].Value, match.Groups.Count == 2 ? null : match.Groups[2].Value);
  21. }
  22. return dict;
  23. }
  24. internal static string Serialize(List<string> list)
  25. {
  26. return "{" + string.Join(",", list.Select(i => $"\"{i}\"")) + "}";
  27. }
  28. internal static List<string> DeserializeList(string json)
  29. {
  30. var list = new List<string>();
  31. foreach (Match match in Regex.Matches(json, "\"(.*?)\""))
  32. {
  33. list.Add(match.Groups[1].Value);
  34. }
  35. return list;
  36. }
  37. }
  38. }