LJParser.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Jayrock.Json.Conversion;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace LJLib.Text.Parser
  6. {
  7. public sealed class LJParser
  8. {
  9. public object JsonParse(Type type, object jsonstr, IDictionary<string, byte[]> files = null)
  10. {
  11. if (type == null)
  12. {
  13. throw new ArgumentNullException("type");
  14. }
  15. if (jsonstr == null)
  16. {
  17. throw new ArgumentNullException("jsonstr");
  18. }
  19. if (typeof(string).IsAssignableFrom(jsonstr.GetType()))
  20. {
  21. return JsonConvert.Import(type, jsonstr as string, files);
  22. }
  23. else
  24. {
  25. return JsonConvert.Import(type, ToJson(jsonstr), files);
  26. }
  27. }
  28. public T JsonParse<T>(object jsonstr, IDictionary<string, byte[]> files = null)
  29. {
  30. return (T)JsonParse(typeof(T), jsonstr, files);
  31. }
  32. public object JsonParse(object jsonstr)
  33. {
  34. if (jsonstr == null)
  35. {
  36. throw new ArgumentNullException("jsonstr");
  37. }
  38. if (typeof(string).IsAssignableFrom(jsonstr.GetType()))
  39. {
  40. return JsonConvert.Import(jsonstr as string);
  41. }
  42. else
  43. {
  44. return JsonConvert.Import(ToJson(jsonstr));
  45. }
  46. }
  47. public string ToJson(object obj, IDictionary<string, byte[]> files = null, string prefix = null)
  48. {
  49. StringBuilder sb = new StringBuilder();
  50. JsonConvert.Export(obj, sb, files, prefix);
  51. return sb.ToString();
  52. }
  53. }
  54. }