PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wojilu/Serialization/JsonParser/ArrayJsonParser.cs

https://bitbucket.org/kingshine/wojilu
C# | 90 lines | 48 code | 24 blank | 18 comment | 14 complexity | 464e910a9008fbafb4b91ba294078c86 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright 2010 www.wojilu.com
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. namespace wojilu.Serialization {
  20. internal class ArrayJsonParser : JsonParserBase {
  21. private List<object> list = new List<object>();
  22. public override Object getResult() {
  23. return list;
  24. }
  25. public ArrayJsonParser( CharSource charSrc )
  26. : base( charSrc ) {
  27. }
  28. protected override void parse() {
  29. charSrc.moveToText();
  30. if (charSrc.getCurrent() != '[') throw ex( "json array must start with [" );
  31. charSrc.moveToText();
  32. if (charSrc.getCurrent() == ']') return;
  33. // 回到[
  34. charSrc.back();
  35. parseOne();
  36. }
  37. private void parseOne() {
  38. charSrc.moveToText();
  39. if (charSrc.getCurrent() == ',') {
  40. charSrc.back();
  41. list.Add( null );
  42. }
  43. else {
  44. charSrc.back();
  45. // 将值加入列表
  46. Object val = moveAndGetParser().getResult();
  47. list.Add( val );
  48. }
  49. // 剩余字符处理
  50. charSrc.moveToText();
  51. char c = charSrc.getCurrent();
  52. if (c == ']') {
  53. return;
  54. }
  55. if (c != ',') throw ex( "json array must seperated with , " );
  56. charSrc.moveToText();
  57. if (charSrc.getCurrent() == ']') {
  58. return;
  59. }
  60. else {
  61. charSrc.back();
  62. parseOne();
  63. }
  64. }
  65. }
  66. }