XmlJsonReader.cs 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Runtime.Serialization.Json
  5. {
  6. using System.Globalization;
  7. using System.IO;
  8. using System.ServiceModel;
  9. using System.Text;
  10. using System.Runtime.Serialization;
  11. using System.Collections.Generic;
  12. using System.Xml;
  13. class XmlJsonReader : XmlBaseReader, IXmlJsonReaderInitializer
  14. {
  15. const int MaxTextChunk = 2048;
  16. static byte[] charType = new byte[256]
  17. {
  18. CharType.None, // 0 (.)
  19. CharType.None, // 1 (.)
  20. CharType.None, // 2 (.)
  21. CharType.None, // 3 (.)
  22. CharType.None, // 4 (.)
  23. CharType.None, // 5 (.)
  24. CharType.None, // 6 (.)
  25. CharType.None, // 7 (.)
  26. CharType.None, // 8 (.)
  27. CharType.None, // 9 (.)
  28. CharType.None, // A (.)
  29. CharType.None, // B (.)
  30. CharType.None, // C (.)
  31. CharType.None, // D (.)
  32. CharType.None, // E (.)
  33. CharType.None, // F (.)
  34. CharType.None, // 10 (.)
  35. CharType.None, // 11 (.)
  36. CharType.None, // 12 (.)
  37. CharType.None, // 13 (.)
  38. CharType.None, // 14 (.)
  39. CharType.None, // 15 (.)
  40. CharType.None, // 16 (.)
  41. CharType.None, // 17 (.)
  42. CharType.None, // 18 (.)
  43. CharType.None, // 19 (.)
  44. CharType.None, // 1A (.)
  45. CharType.None, // 1B (.)
  46. CharType.None, // 1C (.)
  47. CharType.None, // 1D (.)
  48. CharType.None, // 1E (.)
  49. CharType.None, // 1F (.)
  50. CharType.None, // 20 ( )
  51. CharType.None, // 21 (!)
  52. CharType.None, // 22 (")
  53. CharType.None, // 23 (#)
  54. CharType.None, // 24 ($)
  55. CharType.None, // 25 (%)
  56. CharType.None, // 26 (&)
  57. CharType.None, // 27 (')
  58. CharType.None, // 28 (()
  59. CharType.None, // 29 ())
  60. CharType.None, // 2A (*)
  61. CharType.None, // 2B (+)
  62. CharType.None, // 2C (,)
  63. CharType.None | CharType.Name, // 2D (-)
  64. CharType.None | CharType.Name, // 2E (.)
  65. CharType.None, // 2F (/)
  66. CharType.None | CharType.Name, // 30 (0)
  67. CharType.None | CharType.Name, // 31 (1)
  68. CharType.None | CharType.Name, // 32 (2)
  69. CharType.None | CharType.Name, // 33 (3)
  70. CharType.None | CharType.Name, // 34 (4)
  71. CharType.None | CharType.Name, // 35 (5)
  72. CharType.None | CharType.Name, // 36 (6)
  73. CharType.None | CharType.Name, // 37 (7)
  74. CharType.None | CharType.Name, // 38 (8)
  75. CharType.None | CharType.Name, // 39 (9)
  76. CharType.None, // 3A (:)
  77. CharType.None, // 3B (;)
  78. CharType.None, // 3C (<)
  79. CharType.None, // 3D (=)
  80. CharType.None, // 3E (>)
  81. CharType.None, // 3F (?)
  82. CharType.None, // 40 (@)
  83. CharType.None | CharType.FirstName | CharType.Name, // 41 (A)
  84. CharType.None | CharType.FirstName | CharType.Name, // 42 (B)
  85. CharType.None | CharType.FirstName | CharType.Name, // 43 (C)
  86. CharType.None | CharType.FirstName | CharType.Name, // 44 (D)
  87. CharType.None | CharType.FirstName | CharType.Name, // 45 (E)
  88. CharType.None | CharType.FirstName | CharType.Name, // 46 (F)
  89. CharType.None | CharType.FirstName | CharType.Name, // 47 (G)
  90. CharType.None | CharType.FirstName | CharType.Name, // 48 (H)
  91. CharType.None | CharType.FirstName | CharType.Name, // 49 (I)
  92. CharType.None | CharType.FirstName | CharType.Name, // 4A (J)
  93. CharType.None | CharType.FirstName | CharType.Name, // 4B (K)
  94. CharType.None | CharType.FirstName | CharType.Name, // 4C (L)
  95. CharType.None | CharType.FirstName | CharType.Name, // 4D (M)
  96. CharType.None | CharType.FirstName | CharType.Name, // 4E (N)
  97. CharType.None | CharType.FirstName | CharType.Name, // 4F (O)
  98. CharType.None | CharType.FirstName | CharType.Name, // 50 (P)
  99. CharType.None | CharType.FirstName | CharType.Name, // 51 (Q)
  100. CharType.None | CharType.FirstName | CharType.Name, // 52 (R)
  101. CharType.None | CharType.FirstName | CharType.Name, // 53 (S)
  102. CharType.None | CharType.FirstName | CharType.Name, // 54 (T)
  103. CharType.None | CharType.FirstName | CharType.Name, // 55 (U)
  104. CharType.None | CharType.FirstName | CharType.Name, // 56 (V)
  105. CharType.None | CharType.FirstName | CharType.Name, // 57 (W)
  106. CharType.None | CharType.FirstName | CharType.Name, // 58 (X)
  107. CharType.None | CharType.FirstName | CharType.Name, // 59 (Y)
  108. CharType.None | CharType.FirstName | CharType.Name, // 5A (Z)
  109. CharType.None, // 5B ([)
  110. CharType.None, // 5C (\)
  111. CharType.None, // 5D (])
  112. CharType.None, // 5E (^)
  113. CharType.None | CharType.FirstName | CharType.Name, // 5F (_)
  114. CharType.None, // 60 (`)
  115. CharType.None | CharType.FirstName | CharType.Name, // 61 (a)
  116. CharType.None | CharType.FirstName | CharType.Name, // 62 (b)
  117. CharType.None | CharType.FirstName | CharType.Name, // 63 (c)
  118. CharType.None | CharType.FirstName | CharType.Name, // 64 (d)
  119. CharType.None | CharType.FirstName | CharType.Name, // 65 (e)
  120. CharType.None | CharType.FirstName | CharType.Name, // 66 (f)
  121. CharType.None | CharType.FirstName | CharType.Name, // 67 (g)
  122. CharType.None | CharType.FirstName | CharType.Name, // 68 (h)
  123. CharType.None | CharType.FirstName | CharType.Name, // 69 (i)
  124. CharType.None | CharType.FirstName | CharType.Name, // 6A (j)
  125. CharType.None | CharType.FirstName | CharType.Name, // 6B (k)
  126. CharType.None | CharType.FirstName | CharType.Name, // 6C (l)
  127. CharType.None | CharType.FirstName | CharType.Name, // 6D (m)
  128. CharType.None | CharType.FirstName | CharType.Name, // 6E (n)
  129. CharType.None | CharType.FirstName | CharType.Name, // 6F (o)
  130. CharType.None | CharType.FirstName | CharType.Name, // 70 (p)
  131. CharType.None | CharType.FirstName | CharType.Name, // 71 (q)
  132. CharType.None | CharType.FirstName | CharType.Name, // 72 (r)
  133. CharType.None | CharType.FirstName | CharType.Name, // 73 (s)
  134. CharType.None | CharType.FirstName | CharType.Name, // 74 (t)
  135. CharType.None | CharType.FirstName | CharType.Name, // 75 (u)
  136. CharType.None | CharType.FirstName | CharType.Name, // 76 (v)
  137. CharType.None | CharType.FirstName | CharType.Name, // 77 (w)
  138. CharType.None | CharType.FirstName | CharType.Name, // 78 (x)
  139. CharType.None | CharType.FirstName | CharType.Name, // 79 (y)
  140. CharType.None | CharType.FirstName | CharType.Name, // 7A (z)
  141. CharType.None, // 7B ({)
  142. CharType.None, // 7C (|)
  143. CharType.None, // 7D (})
  144. CharType.None, // 7E (~)
  145. CharType.None, // 7F (.)
  146. CharType.None | CharType.FirstName | CharType.Name, // 80 (.)
  147. CharType.None | CharType.FirstName | CharType.Name, // 81 (.)
  148. CharType.None | CharType.FirstName | CharType.Name, // 82 (.)
  149. CharType.None | CharType.FirstName | CharType.Name, // 83 (.)
  150. CharType.None | CharType.FirstName | CharType.Name, // 84 (.)
  151. CharType.None | CharType.FirstName | CharType.Name, // 85 (.)
  152. CharType.None | CharType.FirstName | CharType.Name, // 86 (.)
  153. CharType.None | CharType.FirstName | CharType.Name, // 87 (.)
  154. CharType.None | CharType.FirstName | CharType.Name, // 88 (.)
  155. CharType.None | CharType.FirstName | CharType.Name, // 89 (.)
  156. CharType.None | CharType.FirstName | CharType.Name, // 8A (.)
  157. CharType.None | CharType.FirstName | CharType.Name, // 8B (.)
  158. CharType.None | CharType.FirstName | CharType.Name, // 8C (.)
  159. CharType.None | CharType.FirstName | CharType.Name, // 8D (.)
  160. CharType.None | CharType.FirstName | CharType.Name, // 8E (.)
  161. CharType.None | CharType.FirstName | CharType.Name, // 8F (.)
  162. CharType.None | CharType.FirstName | CharType.Name, // 90 (.)
  163. CharType.None | CharType.FirstName | CharType.Name, // 91 (.)
  164. CharType.None | CharType.FirstName | CharType.Name, // 92 (.)
  165. CharType.None | CharType.FirstName | CharType.Name, // 93 (.)
  166. CharType.None | CharType.FirstName | CharType.Name, // 94 (.)
  167. CharType.None | CharType.FirstName | CharType.Name, // 95 (.)
  168. CharType.None | CharType.FirstName | CharType.Name, // 96 (.)
  169. CharType.None | CharType.FirstName | CharType.Name, // 97 (.)
  170. CharType.None | CharType.FirstName | CharType.Name, // 98 (.)
  171. CharType.None | CharType.FirstName | CharType.Name, // 99 (.)
  172. CharType.None | CharType.FirstName | CharType.Name, // 9A (.)
  173. CharType.None | CharType.FirstName | CharType.Name, // 9B (.)
  174. CharType.None | CharType.FirstName | CharType.Name, // 9C (.)
  175. CharType.None | CharType.FirstName | CharType.Name, // 9D (.)
  176. CharType.None | CharType.FirstName | CharType.Name, // 9E (.)
  177. CharType.None | CharType.FirstName | CharType.Name, // 9F (.)
  178. CharType.None | CharType.FirstName | CharType.Name, // A0 (�)
  179. CharType.None | CharType.FirstName | CharType.Name, // A1 (�)
  180. CharType.None | CharType.FirstName | CharType.Name, // A2 (�)
  181. CharType.None | CharType.FirstName | CharType.Name, // A3 (�)
  182. CharType.None | CharType.FirstName | CharType.Name, // A4 ()
  183. CharType.None | CharType.FirstName | CharType.Name, // A5 (�)
  184. CharType.None | CharType.FirstName | CharType.Name, // A6 (�)
  185. CharType.None | CharType.FirstName | CharType.Name, // A7 ()
  186. CharType.None | CharType.FirstName | CharType.Name, // A8 (")
  187. CharType.None | CharType.FirstName | CharType.Name, // A9 (c)
  188. CharType.None | CharType.FirstName | CharType.Name, // AA (�)
  189. CharType.None | CharType.FirstName | CharType.Name, // AB (�)
  190. CharType.None | CharType.FirstName | CharType.Name, // AC (�)
  191. CharType.None | CharType.FirstName | CharType.Name, // AD (-)
  192. CharType.None | CharType.FirstName | CharType.Name, // AE (r)
  193. CharType.None | CharType.FirstName | CharType.Name, // AF (_)
  194. CharType.None | CharType.FirstName | CharType.Name, // B0 (�)
  195. CharType.None | CharType.FirstName | CharType.Name, // B1 (�)
  196. CharType.None | CharType.FirstName | CharType.Name, // B2 (�)
  197. CharType.None | CharType.FirstName | CharType.Name, // B3 (3)
  198. CharType.None | CharType.FirstName | CharType.Name, // B4 (')
  199. CharType.None | CharType.FirstName | CharType.Name, // B5 (�)
  200. CharType.None | CharType.FirstName | CharType.Name, // B6 ()
  201. CharType.None | CharType.FirstName | CharType.Name, // B7 (�)
  202. CharType.None | CharType.FirstName | CharType.Name, // B8 (,)
  203. CharType.None | CharType.FirstName | CharType.Name, // B9 (1)
  204. CharType.None | CharType.FirstName | CharType.Name, // BA (�)
  205. CharType.None | CharType.FirstName | CharType.Name, // BB (�)
  206. CharType.None | CharType.FirstName | CharType.Name, // BC (�)
  207. CharType.None | CharType.FirstName | CharType.Name, // BD (�)
  208. CharType.None | CharType.FirstName | CharType.Name, // BE (_)
  209. CharType.None | CharType.FirstName | CharType.Name, // BF (�)
  210. CharType.None | CharType.FirstName | CharType.Name, // C0 (A)
  211. CharType.None | CharType.FirstName | CharType.Name, // C1 (A)
  212. CharType.None | CharType.FirstName | CharType.Name, // C2 (A)
  213. CharType.None | CharType.FirstName | CharType.Name, // C3 (A)
  214. CharType.None | CharType.FirstName | CharType.Name, // C4 (�)
  215. CharType.None | CharType.FirstName | CharType.Name, // C5 (�)
  216. CharType.None | CharType.FirstName | CharType.Name, // C6 (�)
  217. CharType.None | CharType.FirstName | CharType.Name, // C7 (�)
  218. CharType.None | CharType.FirstName | CharType.Name, // C8 (E)
  219. CharType.None | CharType.FirstName | CharType.Name, // C9 (�)
  220. CharType.None | CharType.FirstName | CharType.Name, // CA (E)
  221. CharType.None | CharType.FirstName | CharType.Name, // CB (E)
  222. CharType.None | CharType.FirstName | CharType.Name, // CC (I)
  223. CharType.None | CharType.FirstName | CharType.Name, // CD (I)
  224. CharType.None | CharType.FirstName | CharType.Name, // CE (I)
  225. CharType.None | CharType.FirstName | CharType.Name, // CF (I)
  226. CharType.None | CharType.FirstName | CharType.Name, // D0 (D)
  227. CharType.None | CharType.FirstName | CharType.Name, // D1 (�)
  228. CharType.None | CharType.FirstName | CharType.Name, // D2 (O)
  229. CharType.None | CharType.FirstName | CharType.Name, // D3 (O)
  230. CharType.None | CharType.FirstName | CharType.Name, // D4 (O)
  231. CharType.None | CharType.FirstName | CharType.Name, // D5 (O)
  232. CharType.None | CharType.FirstName | CharType.Name, // D6 (�)
  233. CharType.None | CharType.FirstName | CharType.Name, // D7 (x)
  234. CharType.None | CharType.FirstName | CharType.Name, // D8 (O)
  235. CharType.None | CharType.FirstName | CharType.Name, // D9 (U)
  236. CharType.None | CharType.FirstName | CharType.Name, // DA (U)
  237. CharType.None | CharType.FirstName | CharType.Name, // DB (U)
  238. CharType.None | CharType.FirstName | CharType.Name, // DC (�)
  239. CharType.None | CharType.FirstName | CharType.Name, // DD (Y)
  240. CharType.None | CharType.FirstName | CharType.Name, // DE (_)
  241. CharType.None | CharType.FirstName | CharType.Name, // DF (�)
  242. CharType.None | CharType.FirstName | CharType.Name, // E0 (�)
  243. CharType.None | CharType.FirstName | CharType.Name, // E1 (�)
  244. CharType.None | CharType.FirstName | CharType.Name, // E2 (�)
  245. CharType.None | CharType.FirstName | CharType.Name, // E3 (a)
  246. CharType.None | CharType.FirstName | CharType.Name, // E4 (�)
  247. CharType.None | CharType.FirstName | CharType.Name, // E5 (�)
  248. CharType.None | CharType.FirstName | CharType.Name, // E6 (�)
  249. CharType.None | CharType.FirstName | CharType.Name, // E7 (�)
  250. CharType.None | CharType.FirstName | CharType.Name, // E8 (�)
  251. CharType.None | CharType.FirstName | CharType.Name, // E9 (�)
  252. CharType.None | CharType.FirstName | CharType.Name, // EA (�)
  253. CharType.None | CharType.FirstName | CharType.Name, // EB (�)
  254. CharType.None | CharType.FirstName | CharType.Name, // EC (�)
  255. CharType.None | CharType.FirstName | CharType.Name, // ED (�)
  256. CharType.None | CharType.FirstName | CharType.Name, // EE (�)
  257. CharType.None | CharType.FirstName | CharType.Name, // EF (�)
  258. CharType.None | CharType.FirstName | CharType.Name, // F0 (d)
  259. CharType.None | CharType.FirstName | CharType.Name, // F1 (�)
  260. CharType.None | CharType.FirstName | CharType.Name, // F2 (�)
  261. CharType.None | CharType.FirstName | CharType.Name, // F3 (�)
  262. CharType.None | CharType.FirstName | CharType.Name, // F4 (�)
  263. CharType.None | CharType.FirstName | CharType.Name, // F5 (o)
  264. CharType.None | CharType.FirstName | CharType.Name, // F6 (�)
  265. CharType.None | CharType.FirstName | CharType.Name, // F7 (�)
  266. CharType.None | CharType.FirstName | CharType.Name, // F8 (o)
  267. CharType.None | CharType.FirstName | CharType.Name, // F9 (�)
  268. CharType.None | CharType.FirstName | CharType.Name, // FA (�)
  269. CharType.None | CharType.FirstName | CharType.Name, // FB (�)
  270. CharType.None | CharType.FirstName | CharType.Name, // FC (�)
  271. CharType.None | CharType.FirstName | CharType.Name, // FD (y)
  272. CharType.None | CharType.FirstName | CharType.Name, // FE (_)
  273. CharType.None | CharType.FirstName | CharType.Name, // FF (�)
  274. };
  275. bool buffered;
  276. byte[] charactersToSkipOnNextRead;
  277. JsonComplexTextMode complexTextMode = JsonComplexTextMode.None;
  278. bool expectingFirstElementInNonPrimitiveChild;
  279. int maxBytesPerRead;
  280. OnXmlDictionaryReaderClose onReaderClose;
  281. bool readServerTypeElement = false;
  282. int scopeDepth = 0;
  283. JsonNodeType[] scopes;
  284. enum JsonComplexTextMode
  285. {
  286. QuotedText,
  287. NumericalText,
  288. None
  289. };
  290. public override bool CanCanonicalize
  291. {
  292. get
  293. {
  294. return false;
  295. }
  296. }
  297. public override string Value
  298. {
  299. get
  300. {
  301. if (IsAttributeValue && !this.IsLocalName(JsonGlobals.typeString))
  302. {
  303. return UnescapeJsonString(base.Value);
  304. }
  305. return base.Value;
  306. }
  307. }
  308. bool IsAttributeValue
  309. {
  310. get
  311. {
  312. return (this.Node.NodeType == XmlNodeType.Attribute || this.Node is XmlAttributeTextNode);
  313. }
  314. }
  315. bool IsReadingCollection
  316. {
  317. get
  318. {
  319. return ((scopeDepth > 0) && (scopes[scopeDepth] == JsonNodeType.Collection));
  320. }
  321. }
  322. bool IsReadingComplexText
  323. {
  324. get
  325. {
  326. return ((!this.Node.IsAtomicValue) &&
  327. (this.Node.NodeType == XmlNodeType.Text));
  328. }
  329. }
  330. public override void Close()
  331. {
  332. base.Close();
  333. OnXmlDictionaryReaderClose onClose = this.onReaderClose;
  334. this.onReaderClose = null;
  335. ResetState();
  336. if (onClose != null)
  337. {
  338. try
  339. {
  340. onClose(this);
  341. }
  342. catch (Exception e)
  343. {
  344. if (Fx.IsFatal(e))
  345. {
  346. throw;
  347. }
  348. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
  349. }
  350. }
  351. }
  352. public override void EndCanonicalization()
  353. {
  354. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  355. }
  356. public override string GetAttribute(int index)
  357. {
  358. return UnescapeJsonString(base.GetAttribute(index));
  359. }
  360. public override string GetAttribute(string localName, string namespaceUri)
  361. {
  362. if (localName != JsonGlobals.typeString)
  363. {
  364. return UnescapeJsonString(base.GetAttribute(localName, namespaceUri));
  365. }
  366. return base.GetAttribute(localName, namespaceUri);
  367. }
  368. public override string GetAttribute(string name)
  369. {
  370. if (name != JsonGlobals.typeString)
  371. {
  372. return UnescapeJsonString(base.GetAttribute(name));
  373. }
  374. return base.GetAttribute(name);
  375. }
  376. public override string GetAttribute(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  377. {
  378. if (XmlDictionaryString.GetString(localName) != JsonGlobals.typeString)
  379. {
  380. return UnescapeJsonString(base.GetAttribute(localName, namespaceUri));
  381. }
  382. return base.GetAttribute(localName, namespaceUri);
  383. }
  384. public override bool Read()
  385. {
  386. if (this.Node.CanMoveToElement)
  387. {
  388. // If we're positioned on an attribute or attribute text on an empty element, we need to move back
  389. // to the element in order to get the correct setting of ExitScope
  390. MoveToElement();
  391. }
  392. if (this.Node.ReadState == ReadState.Closed)
  393. {
  394. return false;
  395. }
  396. if (this.Node.ExitScope)
  397. {
  398. ExitScope();
  399. }
  400. if (!buffered)
  401. {
  402. BufferReader.SetWindow(ElementNode.BufferOffset, this.maxBytesPerRead);
  403. }
  404. byte ch;
  405. // Skip whitespace before checking EOF
  406. // Complex text check necessary because whitespace could be part of really long
  407. // quoted text that's read using multiple Read() calls.
  408. // This also ensures that we deal with the whitespace-only input case properly by not
  409. // floating a root element at all.
  410. if (!IsReadingComplexText)
  411. {
  412. SkipWhitespaceInBufferReader();
  413. if (TryGetByte(out ch))
  414. {
  415. if (charactersToSkipOnNextRead[0] == ch || charactersToSkipOnNextRead[1] == ch)
  416. {
  417. BufferReader.SkipByte();
  418. charactersToSkipOnNextRead[0] = 0;
  419. charactersToSkipOnNextRead[1] = 0;
  420. }
  421. }
  422. SkipWhitespaceInBufferReader();
  423. if (TryGetByte(out ch))
  424. {
  425. if (ch == JsonGlobals.EndCollectionByte && IsReadingCollection)
  426. {
  427. BufferReader.SkipByte();
  428. SkipWhitespaceInBufferReader();
  429. ExitJsonScope();
  430. }
  431. }
  432. if (BufferReader.EndOfFile)
  433. {
  434. if (scopeDepth > 0)
  435. {
  436. MoveToEndElement();
  437. return true;
  438. }
  439. else
  440. {
  441. MoveToEndOfFile();
  442. return false;
  443. }
  444. }
  445. }
  446. ch = BufferReader.GetByte();
  447. if (scopeDepth == 0)
  448. {
  449. ReadNonExistentElementName(StringHandleConstStringType.Root);
  450. }
  451. else if (IsReadingComplexText)
  452. {
  453. switch (complexTextMode)
  454. {
  455. case JsonComplexTextMode.NumericalText:
  456. ReadNumericalText();
  457. break;
  458. case JsonComplexTextMode.QuotedText:
  459. if (ch == (byte)'\\')
  460. {
  461. ReadEscapedCharacter(true); // moveToText
  462. }
  463. else
  464. {
  465. ReadQuotedText(true); // moveToText
  466. }
  467. break;
  468. case JsonComplexTextMode.None:
  469. XmlExceptionHelper.ThrowXmlException(this,
  470. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)ch)));
  471. break;
  472. }
  473. }
  474. else if (IsReadingCollection)
  475. {
  476. ReadNonExistentElementName(StringHandleConstStringType.Item);
  477. }
  478. else if (ch == JsonGlobals.EndCollectionByte)
  479. {
  480. BufferReader.SkipByte();
  481. MoveToEndElement();
  482. ExitJsonScope();
  483. }
  484. else if (ch == JsonGlobals.ObjectByte)
  485. {
  486. BufferReader.SkipByte();
  487. SkipWhitespaceInBufferReader();
  488. ch = (byte)BufferReader.GetByte();
  489. if (ch == JsonGlobals.EndObjectByte)
  490. {
  491. BufferReader.SkipByte();
  492. SkipWhitespaceInBufferReader();
  493. if (TryGetByte(out ch))
  494. {
  495. if (ch == JsonGlobals.MemberSeparatorByte)
  496. {
  497. BufferReader.SkipByte();
  498. }
  499. }
  500. else
  501. {
  502. charactersToSkipOnNextRead[0] = JsonGlobals.MemberSeparatorByte;
  503. }
  504. MoveToEndElement();
  505. }
  506. else
  507. {
  508. EnterJsonScope(JsonNodeType.Object);
  509. ParseStartElement();
  510. }
  511. }
  512. else if (ch == JsonGlobals.EndObjectByte)
  513. {
  514. BufferReader.SkipByte();
  515. if (expectingFirstElementInNonPrimitiveChild)
  516. {
  517. SkipWhitespaceInBufferReader();
  518. ch = BufferReader.GetByte();
  519. if ((ch == JsonGlobals.MemberSeparatorByte) ||
  520. (ch == JsonGlobals.EndObjectByte))
  521. {
  522. BufferReader.SkipByte();
  523. }
  524. else
  525. {
  526. XmlExceptionHelper.ThrowXmlException(this,
  527. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter,
  528. (char)ch)));
  529. }
  530. expectingFirstElementInNonPrimitiveChild = false;
  531. }
  532. MoveToEndElement();
  533. }
  534. else if (ch == JsonGlobals.MemberSeparatorByte)
  535. {
  536. BufferReader.SkipByte();
  537. MoveToEndElement();
  538. }
  539. else if (ch == JsonGlobals.QuoteByte)
  540. {
  541. if (readServerTypeElement)
  542. {
  543. readServerTypeElement = false;
  544. EnterJsonScope(JsonNodeType.Object);
  545. ParseStartElement();
  546. }
  547. else if (this.Node.NodeType == XmlNodeType.Element)
  548. {
  549. if (expectingFirstElementInNonPrimitiveChild)
  550. {
  551. EnterJsonScope(JsonNodeType.Object);
  552. ParseStartElement();
  553. }
  554. else
  555. {
  556. BufferReader.SkipByte();
  557. ReadQuotedText(true); // moveToText
  558. }
  559. }
  560. else if (this.Node.NodeType == XmlNodeType.EndElement)
  561. {
  562. EnterJsonScope(JsonNodeType.Element);
  563. ParseStartElement();
  564. }
  565. else
  566. {
  567. XmlExceptionHelper.ThrowXmlException(this,
  568. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter,
  569. JsonGlobals.QuoteChar)));
  570. }
  571. }
  572. else if (ch == (byte)'f')
  573. {
  574. int offset;
  575. byte[] buffer = BufferReader.GetBuffer(5, out offset);
  576. if (buffer[offset + 1] != (byte)'a' ||
  577. buffer[offset + 2] != (byte)'l' ||
  578. buffer[offset + 3] != (byte)'s' ||
  579. buffer[offset + 4] != (byte)'e')
  580. {
  581. XmlExceptionHelper.ThrowTokenExpected(this, "false", Encoding.UTF8.GetString(buffer, offset, 5));
  582. }
  583. BufferReader.Advance(5);
  584. if (TryGetByte(out ch))
  585. {
  586. if (!IsWhitespace(ch) && ch != JsonGlobals.MemberSeparatorByte && ch != JsonGlobals.EndObjectChar && ch != JsonGlobals.EndCollectionByte)
  587. {
  588. XmlExceptionHelper.ThrowTokenExpected(this, "false", Encoding.UTF8.GetString(buffer, offset, 4) + (char)ch);
  589. }
  590. }
  591. MoveToAtomicText().Value.SetValue(ValueHandleType.UTF8, offset, 5);
  592. }
  593. else if (ch == (byte)'t')
  594. {
  595. int offset;
  596. byte[] buffer = BufferReader.GetBuffer(4, out offset);
  597. if (buffer[offset + 1] != (byte)'r' ||
  598. buffer[offset + 2] != (byte)'u' ||
  599. buffer[offset + 3] != (byte)'e')
  600. {
  601. XmlExceptionHelper.ThrowTokenExpected(this, "true", Encoding.UTF8.GetString(buffer, offset, 4));
  602. }
  603. BufferReader.Advance(4);
  604. if (TryGetByte(out ch))
  605. {
  606. if (!IsWhitespace(ch) && ch != JsonGlobals.MemberSeparatorByte && ch != JsonGlobals.EndObjectChar && ch != JsonGlobals.EndCollectionByte)
  607. {
  608. XmlExceptionHelper.ThrowTokenExpected(this, "true", Encoding.UTF8.GetString(buffer, offset, 4) + (char)ch);
  609. }
  610. }
  611. MoveToAtomicText().Value.SetValue(ValueHandleType.UTF8, offset, 4);
  612. }
  613. else if (ch == (byte)'n')
  614. {
  615. int offset;
  616. byte[] buffer = BufferReader.GetBuffer(4, out offset);
  617. if (buffer[offset + 1] != (byte)'u' ||
  618. buffer[offset + 2] != (byte)'l' ||
  619. buffer[offset + 3] != (byte)'l')
  620. {
  621. XmlExceptionHelper.ThrowTokenExpected(this, "null", Encoding.UTF8.GetString(buffer, offset, 4));
  622. }
  623. BufferReader.Advance(4);
  624. SkipWhitespaceInBufferReader();
  625. if (TryGetByte(out ch))
  626. {
  627. if (ch == JsonGlobals.MemberSeparatorByte || ch == JsonGlobals.EndObjectChar)
  628. {
  629. BufferReader.SkipByte();
  630. }
  631. else if (ch != JsonGlobals.EndCollectionByte)
  632. {
  633. XmlExceptionHelper.ThrowTokenExpected(this, "null", Encoding.UTF8.GetString(buffer, offset, 4) + (char)ch);
  634. }
  635. }
  636. else
  637. {
  638. charactersToSkipOnNextRead[0] = JsonGlobals.MemberSeparatorByte;
  639. charactersToSkipOnNextRead[1] = JsonGlobals.EndObjectByte;
  640. }
  641. MoveToEndElement();
  642. }
  643. else if ((ch == (byte)'-') ||
  644. (((byte)'0' <= ch) && (ch <= (byte)'9')) ||
  645. (ch == (byte)'I') ||
  646. (ch == (byte)'N'))
  647. {
  648. ReadNumericalText();
  649. }
  650. else
  651. {
  652. XmlExceptionHelper.ThrowXmlException(this,
  653. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)ch)));
  654. }
  655. return true;
  656. }
  657. public override decimal ReadContentAsDecimal()
  658. {
  659. string value = ReadContentAsString();
  660. try
  661. {
  662. return decimal.Parse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo);
  663. }
  664. catch (ArgumentException exception)
  665. {
  666. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "decimal", exception));
  667. }
  668. catch (FormatException exception)
  669. {
  670. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "decimal", exception));
  671. }
  672. catch (OverflowException exception)
  673. {
  674. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "decimal", exception));
  675. }
  676. }
  677. public override int ReadContentAsInt()
  678. {
  679. return ParseInt(ReadContentAsString(), NumberStyles.Float);
  680. }
  681. public override long ReadContentAsLong()
  682. {
  683. string value = ReadContentAsString();
  684. try
  685. {
  686. return long.Parse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo);
  687. }
  688. catch (ArgumentException exception)
  689. {
  690. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int64", exception));
  691. }
  692. catch (FormatException exception)
  693. {
  694. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int64", exception));
  695. }
  696. catch (OverflowException exception)
  697. {
  698. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int64", exception));
  699. }
  700. }
  701. public override int ReadValueAsBase64(byte[] buffer, int offset, int count)
  702. {
  703. if (IsAttributeValue)
  704. {
  705. if (buffer == null)
  706. {
  707. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("buffer"));
  708. }
  709. if (offset < 0)
  710. {
  711. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ValueMustBeNonNegative)));
  712. }
  713. if (offset > buffer.Length)
  714. {
  715. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.OffsetExceedsBufferSize, buffer.Length)));
  716. }
  717. if (count < 0)
  718. {
  719. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ValueMustBeNonNegative)));
  720. }
  721. if (count > buffer.Length - offset)
  722. {
  723. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.SizeExceedsRemainingBufferSpace, buffer.Length - offset)));
  724. }
  725. return 0;
  726. }
  727. return base.ReadValueAsBase64(buffer, offset, count);
  728. }
  729. public override int ReadValueChunk(char[] chars, int offset, int count)
  730. {
  731. if (IsAttributeValue)
  732. {
  733. if (chars == null)
  734. {
  735. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
  736. }
  737. if (offset < 0)
  738. {
  739. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ValueMustBeNonNegative)));
  740. }
  741. if (offset > chars.Length)
  742. {
  743. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.OffsetExceedsBufferSize, chars.Length)));
  744. }
  745. if (count < 0)
  746. {
  747. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.ValueMustBeNonNegative)));
  748. }
  749. if (count > chars.Length - offset)
  750. {
  751. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.SizeExceedsRemainingBufferSpace, chars.Length - offset)));
  752. }
  753. int actual;
  754. string value = UnescapeJsonString(this.Node.ValueAsString);
  755. actual = Math.Min(count, value.Length);
  756. if (actual > 0)
  757. {
  758. value.CopyTo(0, chars, offset, actual);
  759. if (this.Node.QNameType == QNameType.Xmlns)
  760. {
  761. this.Node.Namespace.Uri.SetValue(0, 0);
  762. }
  763. else
  764. {
  765. this.Node.Value.SetValue(ValueHandleType.UTF8, 0, 0);
  766. }
  767. }
  768. return actual;
  769. }
  770. return base.ReadValueChunk(chars, offset, count);
  771. }
  772. public void SetInput(byte[] buffer, int offset, int count, Encoding encoding, XmlDictionaryReaderQuotas quotas,
  773. OnXmlDictionaryReaderClose onClose)
  774. {
  775. if (buffer == null)
  776. {
  777. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
  778. }
  779. if (offset < 0)
  780. {
  781. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  782. new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
  783. }
  784. if (offset > buffer.Length)
  785. {
  786. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  787. new ArgumentOutOfRangeException("offset",
  788. SR.GetString(SR.JsonOffsetExceedsBufferSize, buffer.Length)));
  789. }
  790. if (count < 0)
  791. {
  792. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  793. new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
  794. }
  795. if (count > buffer.Length - offset)
  796. {
  797. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  798. new ArgumentOutOfRangeException("count",
  799. SR.GetString(SR.JsonSizeExceedsRemainingBufferSpace,
  800. buffer.Length - offset)));
  801. }
  802. MoveToInitial(quotas, onClose);
  803. ArraySegment<byte> seg = JsonEncodingStreamWrapper.ProcessBuffer(buffer, offset, count, encoding);
  804. BufferReader.SetBuffer(seg.Array, seg.Offset, seg.Count, null, null);
  805. this.buffered = true;
  806. ResetState();
  807. }
  808. public void SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas,
  809. OnXmlDictionaryReaderClose onClose)
  810. {
  811. if (stream == null)
  812. {
  813. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream");
  814. }
  815. MoveToInitial(quotas, onClose);
  816. stream = new JsonEncodingStreamWrapper(stream, encoding, true);
  817. BufferReader.SetBuffer(stream, null, null);
  818. this.buffered = false;
  819. ResetState();
  820. }
  821. public override void StartCanonicalization(Stream stream, bool includeComments, string[] inclusivePrefixes)
  822. {
  823. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  824. }
  825. internal static void CheckArray(Array array, int offset, int count)
  826. {
  827. if (array == null)
  828. {
  829. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("array"));
  830. }
  831. if (offset < 0)
  832. {
  833. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
  834. }
  835. if (offset > array.Length)
  836. {
  837. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, array.Length)));
  838. }
  839. if (count < 0)
  840. {
  841. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
  842. }
  843. if (count > array.Length - offset)
  844. {
  845. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, array.Length - offset)));
  846. }
  847. }
  848. protected override XmlSigningNodeWriter CreateSigningNodeWriter()
  849. {
  850. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.JsonMethodNotSupported, "CreateSigningNodeWriter")));
  851. }
  852. static int BreakText(byte[] buffer, int offset, int length)
  853. {
  854. // See if we might be breaking a utf8 sequence
  855. if (length > 0 && (buffer[offset + length - 1] & 0x80) == 0x80)
  856. {
  857. // Find the lead char of the utf8 sequence (0x11xxxxxx)
  858. int originalLength = length;
  859. do
  860. {
  861. length--;
  862. } while (length > 0 && (buffer[offset + length] & 0xC0) != 0xC0);
  863. // Couldn't find the lead char
  864. if (length == 0)
  865. {
  866. return originalLength; // Invalid utf8 sequence - can't break
  867. }
  868. // Count how many bytes follow the lead char
  869. byte b = (byte)(buffer[offset + length] << 2);
  870. int byteCount = 2;
  871. while ((b & 0x80) == 0x80)
  872. {
  873. b = (byte)(b << 1);
  874. byteCount++;
  875. // There shouldn't be more than 3 bytes following the lead char
  876. if (byteCount > 4)
  877. {
  878. return originalLength; // Invalid utf8 sequence - can't break
  879. }
  880. }
  881. if (length + byteCount == originalLength)
  882. {
  883. return originalLength; // sequence fits exactly
  884. }
  885. if (length == 0)
  886. {
  887. return originalLength; // Quota too small to read a char
  888. }
  889. }
  890. return length;
  891. }
  892. static int ComputeNumericalTextLength(byte[] buffer, int offset, int offsetMax)
  893. {
  894. int beginOffset = offset;
  895. while (offset < offsetMax)
  896. {
  897. byte ch = buffer[offset];
  898. if (ch == JsonGlobals.MemberSeparatorByte || ch == JsonGlobals.EndObjectByte || ch == JsonGlobals.EndCollectionByte
  899. || IsWhitespace(ch))
  900. {
  901. break;
  902. }
  903. offset++;
  904. }
  905. return offset - beginOffset;
  906. }
  907. static int ComputeQuotedTextLengthUntilEndQuote(byte[] buffer, int offset, int offsetMax, out bool escaped)
  908. {
  909. // Assumes that for quoted text "someText", the first " has been consumed.
  910. // For original text "someText", buffer passed in is someText".
  911. // This method returns return 8 for someText" (s, o, m, e, T, e, x, t).
  912. int beginOffset = offset;
  913. escaped = false;
  914. while (offset < offsetMax)
  915. {
  916. byte ch = buffer[offset];
  917. if (ch < 0x20)
  918. {
  919. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.GetString(SR.InvalidCharacterEncountered, (char)ch)));
  920. }
  921. else if (ch == (byte)'\\' || ch == 0xEF)
  922. {
  923. escaped = true;
  924. break;
  925. }
  926. else if (ch == JsonGlobals.QuoteByte)
  927. {
  928. break;
  929. }
  930. offset++;
  931. }
  932. return offset - beginOffset;
  933. }
  934. // From JSON spec:
  935. // ws = *(
  936. // %x20 / ; Space
  937. // %x09 / ; Horizontal tab
  938. // %x0A / ; Line feed or New line
  939. // %x0D ; Carriage return
  940. // )
  941. static bool IsWhitespace(byte ch)
  942. {
  943. return ((ch == 0x20) || (ch == 0x09) || (ch == 0x0A) || (ch == 0x0D));
  944. }
  945. static char ParseChar(string value, NumberStyles style)
  946. {
  947. int intValue = ParseInt(value, style);
  948. try
  949. {
  950. return Convert.ToChar(intValue);
  951. }
  952. catch (OverflowException exception)
  953. {
  954. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "char", exception));
  955. }
  956. }
  957. static int ParseInt(string value, NumberStyles style)
  958. {
  959. try
  960. {
  961. return int.Parse(value, style, NumberFormatInfo.InvariantInfo);
  962. }
  963. catch (ArgumentException exception)
  964. {
  965. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
  966. }
  967. catch (FormatException exception)
  968. {
  969. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
  970. }
  971. catch (OverflowException exception)
  972. {
  973. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value, "Int32", exception));
  974. }
  975. }
  976. void BufferElement()
  977. {
  978. int elementOffset = BufferReader.Offset;
  979. const int byteCount = 128;
  980. bool done = false;
  981. byte quoteChar = 0;
  982. while (!done)
  983. {
  984. int offset;
  985. int offsetMax;
  986. byte[] buffer = BufferReader.GetBuffer(byteCount, out offset, out offsetMax);
  987. if (offset + byteCount != offsetMax)
  988. {
  989. break;
  990. }
  991. for (int i = offset; i < offsetMax && !done; i++)
  992. {
  993. byte b = buffer[i];
  994. if (b == '\\')
  995. {
  996. i++;
  997. if (i >= offsetMax)
  998. {
  999. break;
  1000. }
  1001. }
  1002. else if (quoteChar == 0)
  1003. {
  1004. if (b == (byte)'\'' || b == JsonGlobals.QuoteByte)
  1005. {
  1006. quoteChar = b;
  1007. }
  1008. if (b == JsonGlobals.NameValueSeparatorByte)
  1009. {
  1010. done = true;
  1011. }
  1012. }
  1013. else
  1014. {
  1015. if (b == quoteChar)
  1016. {
  1017. quoteChar = 0;
  1018. }
  1019. }
  1020. }
  1021. BufferReader.Advance(byteCount);
  1022. }
  1023. BufferReader.Offset = elementOffset;
  1024. }
  1025. void EnterJsonScope(JsonNodeType currentNodeType)
  1026. {
  1027. scopeDepth++;
  1028. if (scopes == null)
  1029. {
  1030. scopes = new JsonNodeType[4];
  1031. }
  1032. else if (scopes.Length == scopeDepth)
  1033. {
  1034. JsonNodeType[] newScopes = new JsonNodeType[scopeDepth * 2];
  1035. Array.Copy(scopes, newScopes, scopeDepth);
  1036. scopes = newScopes;
  1037. }
  1038. scopes[scopeDepth] = currentNodeType;
  1039. }
  1040. JsonNodeType ExitJsonScope()
  1041. {
  1042. JsonNodeType nodeTypeToReturn = scopes[scopeDepth];
  1043. scopes[scopeDepth] = JsonNodeType.None;
  1044. scopeDepth--;
  1045. return nodeTypeToReturn;
  1046. }
  1047. new void MoveToEndElement()
  1048. {
  1049. ExitJsonScope();
  1050. base.MoveToEndElement();
  1051. }
  1052. void MoveToInitial(XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
  1053. {
  1054. MoveToInitial(quotas);
  1055. this.maxBytesPerRead = quotas.MaxBytesPerRead;
  1056. this.onReaderClose = onClose;
  1057. }
  1058. void ParseAndSetLocalName()
  1059. {
  1060. XmlElementNode elementNode = EnterScope();
  1061. elementNode.NameOffset = BufferReader.Offset;
  1062. do
  1063. {
  1064. if (BufferReader.GetByte() == '\\')
  1065. {
  1066. ReadEscapedCharacter(false); // moveToText
  1067. }
  1068. else
  1069. {
  1070. ReadQuotedText(false); // moveToText
  1071. }
  1072. } while (complexTextMode == JsonComplexTextMode.QuotedText);
  1073. int actualOffset = BufferReader.Offset - 1; // -1 to ignore " at end of local name
  1074. elementNode.LocalName.SetValue(elementNode.NameOffset, actualOffset - elementNode.NameOffset);
  1075. elementNode.NameLength = actualOffset - elementNode.NameOffset;
  1076. elementNode.Namespace.Uri.SetValue(elementNode.NameOffset, 0);
  1077. elementNode.Prefix.SetValue(PrefixHandleType.Empty);
  1078. elementNode.IsEmptyElement = false;
  1079. elementNode.ExitScope = false;
  1080. elementNode.BufferOffset = actualOffset;
  1081. int currentCharacter = (int)BufferReader.GetByte(elementNode.NameOffset);
  1082. if ((charType[currentCharacter] & CharType.FirstName) == 0)
  1083. {
  1084. SetJsonNameWithMapping(elementNode);
  1085. }
  1086. else
  1087. {
  1088. for (int i = 0, offset = elementNode.NameOffset; i < elementNode.NameLength; i++, offset++)
  1089. {
  1090. currentCharacter = (int)BufferReader.GetByte(offset);
  1091. if ((charType[currentCharacter] & CharType.Name) == 0 || currentCharacter >= 0x80)
  1092. {
  1093. SetJsonNameWithMapping(elementNode);
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. void ParseStartElement()
  1100. {
  1101. if (!buffered)
  1102. {
  1103. BufferElement();
  1104. }
  1105. expectingFirstElementInNonPrimitiveChild = false;
  1106. byte ch = BufferReader.GetByte();
  1107. if (ch == JsonGlobals.QuoteByte)
  1108. {
  1109. BufferReader.SkipByte();
  1110. ParseAndSetLocalName();
  1111. SkipWhitespaceInBufferReader();
  1112. SkipExpectedByteInBufferReader(JsonGlobals.NameValueSeparatorByte);
  1113. SkipWhitespaceInBufferReader();
  1114. if (BufferReader.GetByte() == JsonGlobals.ObjectByte)
  1115. {
  1116. BufferReader.SkipByte();
  1117. expectingFirstElementInNonPrimitiveChild = true;
  1118. }
  1119. ReadAttributes();
  1120. }
  1121. else
  1122. {
  1123. // " and } are the only two valid characters that may follow a {
  1124. XmlExceptionHelper.ThrowTokenExpected(this, "\"", (char)ch);
  1125. }
  1126. }
  1127. void ReadAttributes()
  1128. {
  1129. XmlAttributeNode attribute = AddAttribute();
  1130. attribute.LocalName.SetConstantValue(StringHandleConstStringType.Type);
  1131. attribute.Namespace.Uri.SetValue(0, 0);
  1132. attribute.Prefix.SetValue(PrefixHandleType.Empty);
  1133. SkipWhitespaceInBufferReader();
  1134. byte nextByte = BufferReader.GetByte();
  1135. switch (nextByte)
  1136. {
  1137. case JsonGlobals.QuoteByte:
  1138. if (!expectingFirstElementInNonPrimitiveChild)
  1139. {
  1140. attribute.Value.SetConstantValue(ValueHandleConstStringType.String);
  1141. }
  1142. else
  1143. {
  1144. attribute.Value.SetConstantValue(ValueHandleConstStringType.Object);
  1145. ReadServerTypeAttribute(true);
  1146. }
  1147. break;
  1148. case (byte)'n':
  1149. attribute.Value.SetConstantValue(ValueHandleConstStringType.Null);
  1150. break;
  1151. case (byte)'t':
  1152. case (byte)'f':
  1153. attribute.Value.SetConstantValue(ValueHandleConstStringType.Boolean);
  1154. break;
  1155. case JsonGlobals.ObjectByte:
  1156. attribute.Value.SetConstantValue(ValueHandleConstStringType.Object);
  1157. ReadServerTypeAttribute(false);
  1158. break;
  1159. case JsonGlobals.EndObjectByte:
  1160. if (expectingFirstElementInNonPrimitiveChild)
  1161. {
  1162. attribute.Value.SetConstantValue(ValueHandleConstStringType.Object);
  1163. }
  1164. else
  1165. {
  1166. XmlExceptionHelper.ThrowXmlException(this,
  1167. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)nextByte)));
  1168. }
  1169. break;
  1170. case JsonGlobals.CollectionByte:
  1171. attribute.Value.SetConstantValue(ValueHandleConstStringType.Array);
  1172. BufferReader.SkipByte();
  1173. EnterJsonScope(JsonNodeType.Collection);
  1174. break;
  1175. default:
  1176. if (nextByte == '-' ||
  1177. (nextByte <= '9' && nextByte >= '0') ||
  1178. nextByte == 'N' ||
  1179. nextByte == 'I')
  1180. {
  1181. attribute.Value.SetConstantValue(ValueHandleConstStringType.Number);
  1182. }
  1183. else
  1184. {
  1185. XmlExceptionHelper.ThrowXmlException(this,
  1186. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)nextByte)));
  1187. }
  1188. break;
  1189. }
  1190. }
  1191. void ReadEscapedCharacter(bool moveToText)
  1192. {
  1193. BufferReader.SkipByte();
  1194. char ch = (char)BufferReader.GetByte();
  1195. if (ch == 'u')
  1196. {
  1197. BufferReader.SkipByte();
  1198. int offset;
  1199. byte[] buffer = BufferReader.GetBuffer(5, out offset);
  1200. string bufferAsString = Encoding.UTF8.GetString(buffer, offset, 4);
  1201. BufferReader.Advance(4);
  1202. int charValue = ParseChar(bufferAsString, NumberStyles.HexNumber);
  1203. if (Char.IsHighSurrogate((char)charValue))
  1204. {
  1205. byte nextByte = BufferReader.GetByte();
  1206. if (nextByte == (byte)'\\')
  1207. {
  1208. BufferReader.SkipByte();
  1209. SkipExpectedByteInBufferReader((byte)'u');
  1210. buffer = BufferReader.GetBuffer(5, out offset);
  1211. bufferAsString = Encoding.UTF8.GetString(buffer, offset, 4);
  1212. BufferReader.Advance(4);
  1213. char lowChar = ParseChar(bufferAsString, NumberStyles.HexNumber);
  1214. if (!Char.IsLowSurrogate(lowChar))
  1215. {
  1216. XmlExceptionHelper.ThrowXmlException(this,
  1217. new XmlException(System.Runtime.Serialization.SR.GetString(System.Runtime.Serialization.SR.XmlInvalidLowSurrogate, bufferAsString)));
  1218. }
  1219. charValue = new SurrogateChar(lowChar, (char)charValue).Char;
  1220. }
  1221. }
  1222. if (buffer[offset + 4] == JsonGlobals.QuoteByte)
  1223. {
  1224. BufferReader.SkipByte();
  1225. if (moveToText)
  1226. {
  1227. MoveToAtomicText().Value.SetCharValue(charValue);
  1228. }
  1229. complexTextMode = JsonComplexTextMode.None;
  1230. }
  1231. else
  1232. {
  1233. if (moveToText)
  1234. {
  1235. MoveToComplexText().Value.SetCharValue(charValue);
  1236. }
  1237. complexTextMode = JsonComplexTextMode.QuotedText;
  1238. }
  1239. }
  1240. else
  1241. {
  1242. switch (ch)
  1243. {
  1244. case 'b':
  1245. ch = '\b';
  1246. break;
  1247. case 'f':
  1248. ch = '\f';
  1249. break;
  1250. case 'n':
  1251. ch = '\n';
  1252. break;
  1253. case 'r':
  1254. ch = '\r';
  1255. break;
  1256. case 't':
  1257. ch = '\t';
  1258. break;
  1259. case '\"':
  1260. case '\\':
  1261. case '/':
  1262. // Do nothing. These are the actual unescaped values.
  1263. break;
  1264. default:
  1265. XmlExceptionHelper.ThrowXmlException(this,
  1266. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, (char)ch)));
  1267. break;
  1268. }
  1269. BufferReader.SkipByte();
  1270. if (BufferReader.GetByte() == JsonGlobals.QuoteByte)
  1271. {
  1272. BufferReader.SkipByte();
  1273. if (moveToText)
  1274. {
  1275. MoveToAtomicText().Value.SetCharValue(ch);
  1276. }
  1277. complexTextMode = JsonComplexTextMode.None;
  1278. }
  1279. else
  1280. {
  1281. if (moveToText)
  1282. {
  1283. MoveToComplexText().Value.SetCharValue(ch);
  1284. }
  1285. complexTextMode = JsonComplexTextMode.QuotedText;
  1286. }
  1287. }
  1288. }
  1289. void ReadNonExistentElementName(StringHandleConstStringType elementName)
  1290. {
  1291. EnterJsonScope(JsonNodeType.Object);
  1292. XmlElementNode elementNode = EnterScope();
  1293. elementNode.LocalName.SetConstantValue(elementName);
  1294. elementNode.Namespace.Uri.SetValue(elementNode.NameOffset, 0);
  1295. elementNode.Prefix.SetValue(PrefixHandleType.Empty);
  1296. elementNode.BufferOffset = BufferReader.Offset;
  1297. elementNode.IsEmptyElement = false;
  1298. elementNode.ExitScope = false;
  1299. ReadAttributes();
  1300. }
  1301. int ReadNonFFFE()
  1302. {
  1303. int off;
  1304. byte[] buff = BufferReader.GetBuffer(3, out off);
  1305. if (buff[off + 1] == 0xBF && (buff[off + 2] == 0xBE || buff[off + 2] == 0xBF))
  1306. {
  1307. XmlExceptionHelper.ThrowXmlException(this, new XmlException(SR.GetString(SR.JsonInvalidFFFE)));
  1308. }
  1309. return 3;
  1310. }
  1311. void ReadNumericalText()
  1312. {
  1313. byte[] buffer;
  1314. int offset;
  1315. int offsetMax;
  1316. int length;
  1317. if (buffered)
  1318. {
  1319. buffer = BufferReader.GetBuffer(out offset, out offsetMax);
  1320. length = ComputeNumericalTextLength(buffer, offset, offsetMax);
  1321. }
  1322. else
  1323. {
  1324. buffer = BufferReader.GetBuffer(MaxTextChunk, out offset, out offsetMax);
  1325. length = ComputeNumericalTextLength(buffer, offset, offsetMax);
  1326. length = BreakText(buffer, offset, length);
  1327. }
  1328. BufferReader.Advance(length);
  1329. if (offset <= offsetMax - length)
  1330. {
  1331. MoveToAtomicText().Value.SetValue(ValueHandleType.UTF8, offset, length);
  1332. complexTextMode = JsonComplexTextMode.None;
  1333. }
  1334. else
  1335. {
  1336. MoveToComplexText().Value.SetValue(ValueHandleType.UTF8, offset, length);
  1337. complexTextMode = JsonComplexTextMode.NumericalText;
  1338. }
  1339. }
  1340. void ReadQuotedText(bool moveToText)
  1341. {
  1342. byte[] buffer;
  1343. int offset;
  1344. int offsetMax;
  1345. int length;
  1346. bool escaped;
  1347. bool endReached;
  1348. if (buffered)
  1349. {
  1350. buffer = BufferReader.GetBuffer(out offset, out offsetMax);
  1351. length = ComputeQuotedTextLengthUntilEndQuote(buffer, offset, offsetMax, out escaped);
  1352. endReached = offset < offsetMax - length;
  1353. }
  1354. else
  1355. {
  1356. buffer = BufferReader.GetBuffer(MaxTextChunk, out offset, out offsetMax);
  1357. length = ComputeQuotedTextLengthUntilEndQuote(buffer, offset, offsetMax, out escaped);
  1358. endReached = offset < offsetMax - length;
  1359. length = BreakText(buffer, offset, length);
  1360. }
  1361. if (escaped && BufferReader.GetByte() == 0xEF)
  1362. {
  1363. offset = BufferReader.Offset;
  1364. length = ReadNonFFFE();
  1365. }
  1366. BufferReader.Advance(length);
  1367. if (!escaped && endReached)
  1368. {
  1369. if (moveToText)
  1370. {
  1371. MoveToAtomicText().Value.SetValue(ValueHandleType.UTF8, offset, length);
  1372. }
  1373. SkipExpectedByteInBufferReader(JsonGlobals.QuoteByte);
  1374. complexTextMode = JsonComplexTextMode.None;
  1375. }
  1376. else
  1377. {
  1378. if ((length == 0) && escaped)
  1379. {
  1380. ReadEscapedCharacter(moveToText);
  1381. }
  1382. else
  1383. {
  1384. if (moveToText)
  1385. {
  1386. MoveToComplexText().Value.SetValue(ValueHandleType.UTF8, offset, length);
  1387. }
  1388. complexTextMode = JsonComplexTextMode.QuotedText;
  1389. }
  1390. }
  1391. }
  1392. void ReadServerTypeAttribute(bool consumedObjectChar)
  1393. {
  1394. if (!consumedObjectChar)
  1395. {
  1396. SkipExpectedByteInBufferReader(JsonGlobals.ObjectByte);
  1397. SkipWhitespaceInBufferReader();
  1398. // we only allow " or } after {
  1399. byte ch = BufferReader.GetByte();
  1400. if (ch != JsonGlobals.QuoteByte && ch != JsonGlobals.EndObjectByte)
  1401. {
  1402. XmlExceptionHelper.ThrowTokenExpected(this, "\"", (char)ch);
  1403. }
  1404. }
  1405. else
  1406. {
  1407. SkipWhitespaceInBufferReader();
  1408. }
  1409. int offset;
  1410. int offsetMax;
  1411. byte[] buffer = BufferReader.GetBuffer(8, out offset, out offsetMax);
  1412. if (offset + 8 <= offsetMax)
  1413. {
  1414. if (buffer[offset + 0] == (byte)'\"' &&
  1415. buffer[offset + 1] == (byte)'_' &&
  1416. buffer[offset + 2] == (byte)'_' &&
  1417. buffer[offset + 3] == (byte)'t' &&
  1418. buffer[offset + 4] == (byte)'y' &&
  1419. buffer[offset + 5] == (byte)'p' &&
  1420. buffer[offset + 6] == (byte)'e' &&
  1421. buffer[offset + 7] == (byte)'\"')
  1422. {
  1423. XmlAttributeNode attribute = AddAttribute();
  1424. attribute.LocalName.SetValue(offset + 1, 6);
  1425. attribute.Namespace.Uri.SetValue(0, 0);
  1426. attribute.Prefix.SetValue(PrefixHandleType.Empty);
  1427. BufferReader.Advance(8);
  1428. if (!buffered)
  1429. {
  1430. BufferElement();
  1431. }
  1432. SkipWhitespaceInBufferReader();
  1433. SkipExpectedByteInBufferReader(JsonGlobals.NameValueSeparatorByte);
  1434. SkipWhitespaceInBufferReader();
  1435. SkipExpectedByteInBufferReader(JsonGlobals.QuoteByte);
  1436. buffer = BufferReader.GetBuffer(out offset, out offsetMax);
  1437. do
  1438. {
  1439. if (BufferReader.GetByte() == '\\')
  1440. {
  1441. ReadEscapedCharacter(false); // moveToText
  1442. }
  1443. else
  1444. {
  1445. ReadQuotedText(false); // moveToText
  1446. }
  1447. } while (complexTextMode == JsonComplexTextMode.QuotedText);
  1448. attribute.Value.SetValue(ValueHandleType.UTF8, offset, BufferReader.Offset - 1 - offset);
  1449. SkipWhitespaceInBufferReader();
  1450. if (BufferReader.GetByte() == JsonGlobals.MemberSeparatorByte)
  1451. {
  1452. BufferReader.SkipByte();
  1453. readServerTypeElement = true;
  1454. }
  1455. }
  1456. }
  1457. if (BufferReader.GetByte() == JsonGlobals.EndObjectByte)
  1458. {
  1459. BufferReader.SkipByte();
  1460. readServerTypeElement = false;
  1461. expectingFirstElementInNonPrimitiveChild = false;
  1462. }
  1463. else
  1464. {
  1465. readServerTypeElement = true;
  1466. }
  1467. }
  1468. void ResetState()
  1469. {
  1470. complexTextMode = JsonComplexTextMode.None;
  1471. expectingFirstElementInNonPrimitiveChild = false;
  1472. charactersToSkipOnNextRead = new byte[2];
  1473. scopeDepth = 0;
  1474. if ((scopes != null) && (scopes.Length > JsonGlobals.maxScopeSize))
  1475. {
  1476. scopes = null;
  1477. }
  1478. }
  1479. void SetJsonNameWithMapping(XmlElementNode elementNode)
  1480. {
  1481. Namespace ns = AddNamespace();
  1482. ns.Prefix.SetValue(PrefixHandleType.A);
  1483. ns.Uri.SetConstantValue(StringHandleConstStringType.Item);
  1484. AddXmlnsAttribute(ns);
  1485. XmlAttributeNode attribute = AddAttribute();
  1486. attribute.LocalName.SetConstantValue(StringHandleConstStringType.Item);
  1487. attribute.Namespace.Uri.SetValue(0, 0);
  1488. attribute.Prefix.SetValue(PrefixHandleType.Empty);
  1489. attribute.Value.SetValue(ValueHandleType.UTF8, elementNode.NameOffset, elementNode.NameLength);
  1490. elementNode.NameLength = 0;
  1491. elementNode.Prefix.SetValue(PrefixHandleType.A);
  1492. elementNode.LocalName.SetConstantValue(StringHandleConstStringType.Item);
  1493. elementNode.Namespace = ns;
  1494. }
  1495. void SkipExpectedByteInBufferReader(byte characterToSkip)
  1496. {
  1497. if (BufferReader.GetByte() != characterToSkip)
  1498. {
  1499. XmlExceptionHelper.ThrowTokenExpected(this, ((char)characterToSkip).ToString(), (char)BufferReader.GetByte());
  1500. }
  1501. BufferReader.SkipByte();
  1502. }
  1503. void SkipWhitespaceInBufferReader()
  1504. {
  1505. byte ch;
  1506. while (TryGetByte(out ch) && IsWhitespace(ch))
  1507. {
  1508. BufferReader.SkipByte();
  1509. }
  1510. }
  1511. bool TryGetByte(out byte ch)
  1512. {
  1513. int offset, offsetMax;
  1514. byte[] buffer = BufferReader.GetBuffer(1, out offset, out offsetMax);
  1515. if (offset < offsetMax)
  1516. {
  1517. ch = buffer[offset];
  1518. return true;
  1519. }
  1520. else
  1521. {
  1522. ch = (byte)'\0';
  1523. return false;
  1524. }
  1525. }
  1526. string UnescapeJsonString(string val)
  1527. {
  1528. if (val == null)
  1529. {
  1530. return null;
  1531. }
  1532. StringBuilder sb = null;
  1533. int startIndex = 0, count = 0;
  1534. for (int i = 0; i < val.Length; i++)
  1535. {
  1536. if (val[i] == '\\')
  1537. {
  1538. i++;
  1539. if (sb == null)
  1540. {
  1541. sb = new StringBuilder();
  1542. }
  1543. sb.Append(val, startIndex, count);
  1544. Fx.Assert(i < val.Length, "Found that an '\' was the last character in a string. ReadServerTypeAttriute validates that the escape sequence is valid when it calls ReadQuotedText and ReadEscapedCharacter");
  1545. if (i >= val.Length)
  1546. {
  1547. XmlExceptionHelper.ThrowXmlException(this, new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, val[i])));
  1548. }
  1549. switch (val[i])
  1550. {
  1551. case '"':
  1552. case '\'':
  1553. case '/':
  1554. case '\\':
  1555. sb.Append(val[i]);
  1556. break;
  1557. case 'b':
  1558. sb.Append('\b');
  1559. break;
  1560. case 'f':
  1561. sb.Append('\f');
  1562. break;
  1563. case 'n':
  1564. sb.Append('\n');
  1565. break;
  1566. case 'r':
  1567. sb.Append('\r');
  1568. break;
  1569. case 't':
  1570. sb.Append('\t');
  1571. break;
  1572. case 'u':
  1573. if ((i + 3) >= val.Length)
  1574. {
  1575. XmlExceptionHelper.ThrowXmlException(this,
  1576. new XmlException(SR.GetString(SR.JsonEncounteredUnexpectedCharacter, val[i])));
  1577. }
  1578. sb.Append(ParseChar(val.Substring(i + 1, 4), NumberStyles.HexNumber));
  1579. i += 4;
  1580. break;
  1581. }
  1582. startIndex = i + 1;
  1583. count = 0;
  1584. }
  1585. else
  1586. {
  1587. count++;
  1588. }
  1589. }
  1590. if (sb == null)
  1591. {
  1592. return val;
  1593. }
  1594. if (count > 0)
  1595. {
  1596. sb.Append(val, startIndex, count);
  1597. }
  1598. return sb.ToString();
  1599. }
  1600. static class CharType
  1601. {
  1602. public const byte FirstName = 0x01;
  1603. public const byte Name = 0x02;
  1604. public const byte None = 0x00;
  1605. }
  1606. }
  1607. }