1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439 |
- // PetaJson v0.5 - A simple but flexible Json library in a single .cs file.
- //
- // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
- // except in compliance with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software distributed under the
- // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- // either express or implied. See the License for the specific language governing permissions
- // and limitations under the License.
- // Define PETAJSON_NO_DYNAMIC to disable Expando support
- // Define PETAJSON_NO_EMIT to disable Reflection.Emit
- // Define PETAJSON_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Reflection;
- using System.Globalization;
- using System.Collections;
- using System.Threading;
- #if !PETAJSON_NO_DYNAMIC
- using System.Dynamic;
- #endif
- #if !PETAJSON_NO_EMIT
- using System.Reflection.Emit;
- #endif
- #if !PETAJSON_NO_DATACONTRACT
- using System.Runtime.Serialization;
- #endif
- namespace PetaJson
- {
- // Pass to format/write/parse functions to override defaults
- [Flags]
- public enum JsonOptions
- {
- None = 0,
- WriteWhitespace = 0x00000001,
- DontWriteWhitespace = 0x00000002,
- StrictParser = 0x00000004,
- NonStrictParser = 0x00000008,
- Flush = 0x00000010,
- AutoSavePreviousVersion = 0x00000020, // Use "SavePreviousVersions" static property
- SavePreviousVersion = 0x00000040, // Always save previous version
- }
- // API
- public static class Json
- {
- static Json()
- {
- WriteWhitespaceDefault = true;
- StrictParserDefault = false;
- #if !PETAJSON_NO_EMIT
- Json.SetFormatterResolver(Internal.Emit.MakeFormatter);
- Json.SetParserResolver(Internal.Emit.MakeParser);
- Json.SetIntoParserResolver(Internal.Emit.MakeIntoParser);
- #endif
- }
- // Pretty format default
- public static bool WriteWhitespaceDefault
- {
- get;
- set;
- }
- // Strict parser
- public static bool StrictParserDefault
- {
- get;
- set;
- }
- // Write an object to a text writer
- public static void Write(TextWriter w, object o, JsonOptions options = JsonOptions.None)
- {
- var writer = new Internal.Writer(w, ResolveOptions(options));
- writer.WriteValue(o);
- }
- static void DeleteFile(string filename)
- {
- try
- {
- System.IO.File.Delete(filename);
- }
- catch
- {
- // Don't care
- }
- }
- public static bool SavePreviousVersions
- {
- get;
- set;
- }
- // Write a file atomically by writing to a temp file and then renaming it - prevents corrupted files if crash
- // in middle of writing file.
- public static void WriteFileAtomic(string filename, object o, JsonOptions options = JsonOptions.None, string backupFilename = null)
- {
- var tempName = filename + ".tmp";
- try
- {
- // Write the temp file
- WriteFile(tempName, o, (options | JsonOptions.Flush));
- if (System.IO.File.Exists(filename))
- {
- bool savePreviousVersion = false;
- if ((options & JsonOptions.AutoSavePreviousVersion)!=0)
- {
- savePreviousVersion = SavePreviousVersions;
- }
- else if ((options & JsonOptions.SavePreviousVersion)!=0)
- {
- savePreviousVersion = true;
- }
- // Work out backup filename
- if (savePreviousVersion)
- {
- // Make sure have a backup filename
- if (backupFilename == null)
- {
- backupFilename = filename + ".previous";
- }
- }
- else
- {
- // No backup
- backupFilename = null;
- }
- // Replace it
- int retry = 0;
- while (true)
- {
- try
- {
- File.Replace(tempName, filename, backupFilename);
- break;
- }
- catch (System.IO.IOException x)
- {
- retry++;
- if (retry >= 5)
- {
- throw new System.IO.IOException(string.Format("Failed to replace temp file {0} with {1} and backup {2}, reason {3}", tempName, filename, backupFilename, x.Message), x);
- }
- System.Threading.Thread.Sleep(2000);
- }
- }
- }
- else
- {
- // Rename it
- File.Move(tempName, filename);
- }
- }
- catch
- {
- DeleteFile(tempName);
- throw;
- }
- }
- // Write an object to a file
- public static void WriteFile(string filename, object o, JsonOptions options = JsonOptions.None)
- {
- using (var w = new StreamWriter(filename))
- {
- Write(w, o, options);
- if ((options & JsonOptions.Flush) != 0)
- {
- w.Flush();
- w.BaseStream.Flush();
- }
- }
- }
- // Format an object as a json string
- public static string Format(object o, JsonOptions options = JsonOptions.None)
- {
- var sw = new StringWriter();
- var writer = new Internal.Writer(sw, ResolveOptions(options));
- writer.WriteValue(o);
- return sw.ToString();
- }
- // Parse an object of specified type from a text reader
- public static object Parse(TextReader r, Type type, JsonOptions options = JsonOptions.None)
- {
- Internal.Reader reader = null;
- try
- {
- reader = new Internal.Reader(r, ResolveOptions(options));
- var retv = reader.Parse(type);
- reader.CheckEOF();
- return retv;
- }
- catch (Exception x)
- {
- var loc = reader == null ? new JsonLineOffset() : reader.CurrentTokenPosition;
- Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader.Context, x.ToString());
- throw new JsonParseException(x, reader.Context, loc);
- }
- }
- // Parse an object of specified type from a text reader
- public static T Parse<T>(TextReader r, JsonOptions options = JsonOptions.None)
- {
- return (T)Parse(r, typeof(T), options);
- }
- // Parse from text reader into an already instantied object
- public static void ParseInto(TextReader r, Object into, JsonOptions options = JsonOptions.None)
- {
- if (into == null)
- throw new NullReferenceException();
- if (into.GetType().IsValueType)
- throw new InvalidOperationException("Can't ParseInto a value type");
- Internal.Reader reader = null;
- try
- {
- reader = new Internal.Reader(r, ResolveOptions(options));
- reader.ParseInto(into);
- reader.CheckEOF();
- }
- catch (Exception x)
- {
- var loc = reader == null ? new JsonLineOffset() : reader.CurrentTokenPosition;
- Console.WriteLine("Exception thrown while parsing JSON at {0}, context:{1}\n{2}", loc, reader.Context, x.ToString());
- throw new JsonParseException(x,reader.Context,loc);
- }
- }
- // Parse an object of specified type from a file
- public static object ParseFile(string filename, Type type, JsonOptions options = JsonOptions.None)
- {
- using (var r = new StreamReader(filename))
- {
- return Parse(r, type, options);
- }
- }
- // Parse an object of specified type from a file
- public static T ParseFile<T>(string filename, JsonOptions options = JsonOptions.None)
- {
- using (var r = new StreamReader(filename))
- {
- return Parse<T>(r, options);
- }
- }
- // Parse from file into an already instantied object
- public static void ParseFileInto(string filename, Object into, JsonOptions options = JsonOptions.None)
- {
- using (var r = new StreamReader(filename))
- {
- ParseInto(r, into, options);
- }
- }
- // Parse an object from a string
- public static object Parse(string data, Type type, JsonOptions options = JsonOptions.None)
- {
- return Parse(new StringReader(data), type, options);
- }
- // Parse an object from a string
- public static T Parse<T>(string data, JsonOptions options = JsonOptions.None)
- {
- return (T)Parse<T>(new StringReader(data), options);
- }
- // Parse from string into an already instantiated object
- public static void ParseInto(string data, Object into, JsonOptions options = JsonOptions.None)
- {
- ParseInto(new StringReader(data), into, options);
- }
- // Create a clone of an object
- public static T Clone<T>(T source)
- {
- return (T)Reparse(source.GetType(), source);
- }
- // Create a clone of an object (untyped)
- public static object Clone(object source)
- {
- return Reparse(source.GetType(), source);
- }
- // Clone an object into another instance
- public static void CloneInto(object dest, object source)
- {
- ReparseInto(dest, source);
- }
- // Reparse an object by writing to a stream and re-reading (possibly
- // as a different type).
- public static object Reparse(Type type, object source)
- {
- if (source == null)
- return null;
- var ms = new MemoryStream();
- try
- {
- // Write
- var w = new StreamWriter(ms);
- Json.Write(w, source);
- w.Flush();
- // Read
- ms.Seek(0, SeekOrigin.Begin);
- var r = new StreamReader(ms);
- return Json.Parse(r, type);
- }
- finally
- {
- ms.Dispose();
- }
- }
- // Typed version of above
- public static T Reparse<T>(object source)
- {
- return (T)Reparse(typeof(T), source);
- }
- // Reparse one object into another object
- public static void ReparseInto(object dest, object source)
- {
- var ms = new MemoryStream();
- try
- {
- // Write
- var w = new StreamWriter(ms);
- Json.Write(w, source);
- w.Flush();
- // Read
- ms.Seek(0, SeekOrigin.Begin);
- var r = new StreamReader(ms);
- Json.ParseInto(r, dest);
- }
- finally
- {
- ms.Dispose();
- }
- }
- // Register a callback that can format a value of a particular type into json
- public static void RegisterFormatter(Type type, Action<IJsonWriter, object> formatter)
- {
- Internal.Writer._formatters[type] = formatter;
- }
- // Typed version of above
- public static void RegisterFormatter<T>(Action<IJsonWriter, T> formatter)
- {
- RegisterFormatter(typeof(T), (w, o) => formatter(w, (T)o));
- }
- // Register a parser for a specified type
- public static void RegisterParser(Type type, Func<IJsonReader, Type, object> parser)
- {
- Internal.Reader._parsers.Set(type, parser);
- }
- // Register a typed parser
- public static void RegisterParser<T>(Func<IJsonReader, Type, T> parser)
- {
- RegisterParser(typeof(T), (r, t) => parser(r, t));
- }
- // Simpler version for simple types
- public static void RegisterParser(Type type, Func<object, object> parser)
- {
- RegisterParser(type, (r, t) => r.ReadLiteral(parser));
- }
- // Simpler and typesafe parser for simple types
- public static void RegisterParser<T>(Func<object, T> parser)
- {
- RegisterParser(typeof(T), literal => parser(literal));
- }
- // Register an into parser
- public static void RegisterIntoParser(Type type, Action<IJsonReader, object> parser)
- {
- Internal.Reader._intoParsers.Set(type, parser);
- }
- // Register an into parser
- public static void RegisterIntoParser<T>(Action<IJsonReader, object> parser)
- {
- RegisterIntoParser(typeof(T), parser);
- }
- // Register a factory for instantiating objects (typically abstract classes)
- // Callback will be invoked for each key in the dictionary until it returns an object
- // instance and which point it will switch to serialization using reflection
- public static void RegisterTypeFactory(Type type, Func<IJsonReader, string, object> factory)
- {
- Internal.Reader._typeFactories.Set(type, factory);
- }
- // Register a callback to provide a formatter for a newly encountered type
- public static void SetFormatterResolver(Func<Type, Action<IJsonWriter, object>> resolver)
- {
- Internal.Writer._formatterResolver = resolver;
- }
- // Register a callback to provide a parser for a newly encountered value type
- public static void SetParserResolver(Func<Type, Func<IJsonReader, Type, object>> resolver)
- {
- Internal.Reader._parserResolver = resolver;
- }
- // Register a callback to provide a parser for a newly encountered reference type
- public static void SetIntoParserResolver(Func<Type, Action<IJsonReader, object>> resolver)
- {
- Internal.Reader._intoParserResolver = resolver;
- }
- public static bool WalkPath(this IDictionary<string, object> This, string Path, bool create, Func<IDictionary<string,object>,string, bool> leafCallback)
- {
- // Walk the path
- var parts = Path.Split('.');
- for (int i = 0; i < parts.Length-1; i++)
- {
- object val;
- if (!This.TryGetValue(parts[i], out val))
- {
- if (!create)
- return false;
- val = new Dictionary<string, object>();
- This[parts[i]] = val;
- }
- This = (IDictionary<string,object>)val;
- }
- // Process the leaf
- return leafCallback(This, parts[parts.Length-1]);
- }
- public static bool PathExists(this IDictionary<string, object> This, string Path)
- {
- return This.WalkPath(Path, false, (dict, key) => dict.ContainsKey(key));
- }
- public static object GetPath(this IDictionary<string, object> This, Type type, string Path, object def)
- {
- This.WalkPath(Path, false, (dict, key) =>
- {
- object val;
- if (dict.TryGetValue(key, out val))
- {
- if (val == null)
- def = val;
- else if (type.IsAssignableFrom(val.GetType()))
- def = val;
- else
- def = Reparse(type, val);
- }
- return true;
- });
- return def;
- }
- // Ensure there's an object of type T at specified path
- public static T GetObjectAtPath<T>(this IDictionary<string, object> This, string Path) where T:class,new()
- {
- T retVal = null;
- This.WalkPath(Path, true, (dict, key) =>
- {
- object val;
- dict.TryGetValue(key, out val);
- retVal = val as T;
- if (retVal == null)
- {
- retVal = val == null ? new T() : Reparse<T>(val);
- dict[key] = retVal;
- }
- return true;
- });
- return retVal;
- }
- public static T GetPath<T>(this IDictionary<string, object> This, string Path, T def = default(T))
- {
- return (T)This.GetPath(typeof(T), Path, def);
- }
- public static void SetPath(this IDictionary<string, object> This, string Path, object value)
- {
- This.WalkPath(Path, true, (dict, key) => { dict[key] = value; return true; });
- }
- // Resolve passed options
- static JsonOptions ResolveOptions(JsonOptions options)
- {
- JsonOptions resolved = JsonOptions.None;
- if ((options & (JsonOptions.WriteWhitespace|JsonOptions.DontWriteWhitespace))!=0)
- resolved |= options & (JsonOptions.WriteWhitespace | JsonOptions.DontWriteWhitespace);
- else
- resolved |= WriteWhitespaceDefault ? JsonOptions.WriteWhitespace : JsonOptions.DontWriteWhitespace;
- if ((options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser)) != 0)
- resolved |= options & (JsonOptions.StrictParser | JsonOptions.NonStrictParser);
- else
- resolved |= StrictParserDefault ? JsonOptions.StrictParser : JsonOptions.NonStrictParser;
- return resolved;
- }
- }
- // Called before loading via reflection
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonLoading
- {
- void OnJsonLoading(IJsonReader r);
- }
- // Called after loading via reflection
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonLoaded
- {
- void OnJsonLoaded(IJsonReader r);
- }
- // Called for each field while loading from reflection
- // Return true if handled
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonLoadField
- {
- bool OnJsonField(IJsonReader r, string key);
- }
- // Called when about to write using reflection
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonWriting
- {
- void OnJsonWriting(IJsonWriter w);
- }
- // Called after written using reflection
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonWritten
- {
- void OnJsonWritten(IJsonWriter w);
- }
- // Describes the current literal in the json stream
- public enum LiteralKind
- {
- None,
- String,
- Null,
- True,
- False,
- SignedInteger,
- UnsignedInteger,
- FloatingPoint,
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public enum Token
- {
- EOF,
- Identifier,
- Literal,
- OpenBrace,
- CloseBrace,
- OpenSquare,
- CloseSquare,
- Equal,
- Colon,
- SemiColon,
- Comma,
- }
- // Passed to registered parsers
- [Obfuscation(Exclude=true, ApplyToMembers=true)]
- public interface IJsonReader
- {
- object Parse(Type type);
- T Parse<T>();
- void ParseInto(object into);
- Token CurrentToken { get; }
- object ReadLiteral(Func<object, object> converter);
- void ParseDictionary(Action<string> callback);
- void ParseArray(Action callback);
- LiteralKind GetLiteralKind();
- string GetLiteralString();
- void NextToken();
- }
- // Passed to registered formatters
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- public interface IJsonWriter
- {
- void WriteStringLiteral(string str);
- void WriteRaw(string str);
- void WriteArray(Action callback);
- void WriteDictionary(Action callback);
- void WriteValue(object value);
- void WriteElement();
- void WriteKey(string key);
- void WriteKeyNoEscaping(string key);
- }
- // Exception thrown for any parse error
- public class JsonParseException : Exception
- {
- public JsonParseException(Exception inner, string context, JsonLineOffset position) :
- base(string.Format("JSON parse error at {0}{1} - {2}", position, string.IsNullOrEmpty(context) ? "" : string.Format(", context {0}", context), inner.Message), inner)
- {
- Position = position;
- Context = context;
- }
- public JsonLineOffset Position;
- public string Context;
- }
- // Represents a line and character offset position in the source Json
- public struct JsonLineOffset
- {
- public int Line;
- public int Offset;
- public override string ToString()
- {
- return string.Format("line {0}, character {1}", Line + 1, Offset + 1);
- }
- }
- // Used to decorate fields and properties that should be serialized
- //
- // - [Json] on class or struct causes all public fields and properties to be serialized
- // - [Json] on a public or non-public field or property causes that member to be serialized
- // - [JsonExclude] on a field or property causes that field to be not serialized
- // - A class or struct with no [Json] attribute has all public fields/properties serialized
- // - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
- //
- // Use [Json("keyname")] to explicitly specify the key to be used
- // [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
- //
- // [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
- //
- // You can also use the system supplied DataContract and DataMember attributes. They'll only be used if there
- // are no PetaJson attributes on the class or it's members. You must specify DataContract on the type and
- // DataMember on any fields/properties that require serialization. There's no need for exclude attribute.
- // When using DataMember, the name of the field or property is used as is - the first letter is left in upper case
- //
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
- public class JsonAttribute : Attribute
- {
- public JsonAttribute()
- {
- _key = null;
- }
- public JsonAttribute(string key)
- {
- _key = key;
- }
- // Key used to save this field/property
- string _key;
- public string Key
- {
- get { return _key; }
- }
- // If true uses ParseInto to parse into the existing object instance
- // If false, creates a new instance as assigns it to the property
- public bool KeepInstance
- {
- get;
- set;
- }
- // If true, the property will be loaded, but not saved
- // Use to upgrade deprecated persisted settings, but not
- // write them back out again
- public bool Deprecated
- {
- get;
- set;
- }
- }
- // See comments for JsonAttribute above
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
- public class JsonExcludeAttribute : Attribute
- {
- public JsonExcludeAttribute()
- {
- }
- }
- // Apply to enum values to specify which enum value to select
- // if the supplied json value doesn't match any.
- // If not found throws an exception
- // eg, any unknown values in the json will be mapped to Fruit.unknown
- //
- // [JsonUnknown(Fruit.unknown)]
- // enum Fruit
- // {
- // unknown,
- // Apple,
- // Pear,
- // }
- [AttributeUsage(AttributeTargets.Enum)]
- public class JsonUnknownAttribute : Attribute
- {
- public JsonUnknownAttribute(object unknownValue)
- {
- UnknownValue = unknownValue;
- }
- public object UnknownValue
- {
- get;
- private set;
- }
- }
- namespace Internal
- {
- class CancelReaderException : Exception
- { }
- // Helper to create instances but include the type name in the thrown exception
- public static class DecoratingActivator
- {
- public static object CreateInstance(Type t)
- {
- try
- {
- return Activator.CreateInstance(t);
- }
- catch (Exception x)
- {
- throw new InvalidOperationException(string.Format("Failed to create instance of type '{0}'", t.FullName), x);
- }
- }
- }
- public class Reader : IJsonReader
- {
- static Reader()
- {
- // Setup default resolvers
- _parserResolver = ResolveParser;
- _intoParserResolver = ResolveIntoParser;
- Func<IJsonReader, Type, object> simpleConverter = (reader, type) =>
- {
- return reader.ReadLiteral(literal => Convert.ChangeType(literal, type, CultureInfo.InvariantCulture));
- };
- Func<IJsonReader, Type, object> numberConverter = (reader, type) =>
- {
- switch (reader.GetLiteralKind())
- {
- case LiteralKind.SignedInteger:
- case LiteralKind.UnsignedInteger:
- {
- var str = reader.GetLiteralString();
- if (str.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
- {
- var tempValue = Convert.ToUInt64(str.Substring(2), 16);
- object val = Convert.ChangeType(tempValue, type, CultureInfo.InvariantCulture);
- reader.NextToken();
- return val;
- }
- else
- {
- object val = Convert.ChangeType(str, type, CultureInfo.InvariantCulture);
- reader.NextToken();
- return val;
- }
- }
- case LiteralKind.FloatingPoint:
- {
- object val = Convert.ChangeType(reader.GetLiteralString(), type, CultureInfo.InvariantCulture);
- reader.NextToken();
- return val;
- }
- }
- throw new InvalidDataException("expected a numeric literal");
- };
- // Default type handlers
- _parsers.Set(typeof(string), simpleConverter);
- _parsers.Set(typeof(char), simpleConverter);
- _parsers.Set(typeof(bool), simpleConverter);
- _parsers.Set(typeof(byte), numberConverter);
- _parsers.Set(typeof(sbyte), numberConverter);
- _parsers.Set(typeof(short), numberConverter);
- _parsers.Set(typeof(ushort), numberConverter);
- _parsers.Set(typeof(int), numberConverter);
- _parsers.Set(typeof(uint), numberConverter);
- _parsers.Set(typeof(long), numberConverter);
- _parsers.Set(typeof(ulong), numberConverter);
- _parsers.Set(typeof(decimal), numberConverter);
- _parsers.Set(typeof(float), numberConverter);
- _parsers.Set(typeof(double), numberConverter);
- _parsers.Set(typeof(DateTime), (reader, type) =>
- {
- return reader.ReadLiteral(literal => Utils.FromUnixMilliseconds((long)Convert.ChangeType(literal, typeof(long), CultureInfo.InvariantCulture)));
- });
- _parsers.Set(typeof(byte[]), (reader, type) =>
- {
- if (reader.CurrentToken == Token.OpenSquare)
- throw new CancelReaderException();
- return reader.ReadLiteral(literal => Convert.FromBase64String((string)Convert.ChangeType(literal, typeof(string), CultureInfo.InvariantCulture)));
- });
- }
- public Reader(TextReader r, JsonOptions options)
- {
- _tokenizer = new Tokenizer(r, options);
- _options = options;
- }
- Tokenizer _tokenizer;
- JsonOptions _options;
- List<string> _contextStack = new List<string>();
- public string Context
- {
- get
- {
- return string.Join(".", _contextStack);
- }
- }
- static Action<IJsonReader, object> ResolveIntoParser(Type type)
- {
- var ri = ReflectionInfo.GetReflectionInfo(type);
- if (ri != null)
- return ri.ParseInto;
- else
- return null;
- }
- static Func<IJsonReader, Type, object> ResolveParser(Type type)
- {
- // See if the Type has a static parser method - T ParseJson(IJsonReader)
- var parseJson = ReflectionInfo.FindParseJson(type);
- if (parseJson != null)
- {
- if (parseJson.GetParameters()[0].ParameterType == typeof(IJsonReader))
- {
- return (r, t) => parseJson.Invoke(null, new Object[] { r });
- }
- else
- {
- return (r, t) =>
- {
- if (r.GetLiteralKind() == LiteralKind.String)
- {
- var o = parseJson.Invoke(null, new Object[] { r.GetLiteralString() });
- r.NextToken();
- return o;
- }
- throw new InvalidDataException(string.Format("Expected string literal for type {0}", type.FullName));
- };
- }
- }
- return (r, t) =>
- {
- var into = DecoratingActivator.CreateInstance(type);
- r.ParseInto(into);
- return into;
- };
- }
- public JsonLineOffset CurrentTokenPosition
- {
- get { return _tokenizer.CurrentTokenPosition; }
- }
- public Token CurrentToken
- {
- get { return _tokenizer.CurrentToken; }
- }
- // ReadLiteral is implemented with a converter callback so that any
- // errors on converting to the target type are thrown before the tokenizer
- // is advanced to the next token. This ensures error location is reported
- // at the start of the literal, not the following token.
- public object ReadLiteral(Func<object, object> converter)
- {
- _tokenizer.Check(Token.Literal);
- var retv = converter(_tokenizer.LiteralValue);
- _tokenizer.NextToken();
- return retv;
- }
- public void CheckEOF()
- {
- _tokenizer.Check(Token.EOF);
- }
- public object Parse(Type type)
- {
- // Null?
- if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.Null)
- {
- _tokenizer.NextToken();
- return null;
- }
- // Handle nullable types
- var typeUnderlying = Nullable.GetUnderlyingType(type);
- if (typeUnderlying != null)
- type = typeUnderlying;
- // See if we have a reader
- Func<IJsonReader, Type, object> parser;
- if (Reader._parsers.TryGetValue(type, out parser))
- {
- try
- {
- return parser(this, type);
- }
- catch (CancelReaderException)
- {
- // Reader aborted trying to read this format
- }
- }
- // See if we have factory
- Func<IJsonReader, string, object> factory;
- if (Reader._typeFactories.TryGetValue(type, out factory))
- {
- // Try first without passing dictionary keys
- object into = factory(this, null);
- if (into == null)
- {
- // This is a awkward situation. The factory requires a value from the dictionary
- // in order to create the target object (typically an abstract class with the class
- // kind recorded in the Json). Since there's no guarantee of order in a json dictionary
- // we can't assume the required key is first.
- // So, create a bookmark on the tokenizer, read keys until the factory returns an
- // object instance and then rewind the tokenizer and continue
- // Create a bookmark so we can rewind
- _tokenizer.CreateBookmark();
- // Skip the opening brace
- _tokenizer.Skip(Token.OpenBrace);
- // First pass to work out type
- ParseDictionaryKeys(key =>
- {
- // Try to instantiate the object
- into = factory(this, key);
- return into == null;
- });
- // Move back to start of the dictionary
- _tokenizer.RewindToBookmark();
- // Quit if still didn't get an object from the factory
- if (into == null)
- throw new InvalidOperationException("Factory didn't create object instance (probably due to a missing key in the Json)");
- }
- // Second pass
- ParseInto(into);
- // Done
- return into;
- }
- // Do we already have an into parser?
- Action<IJsonReader, object> intoParser;
- if (Reader._intoParsers.TryGetValue(type, out intoParser))
- {
- var into = DecoratingActivator.CreateInstance(type);
- ParseInto(into);
- return into;
- }
- // Enumerated type?
- if (type.IsEnum)
- {
- if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
- return ReadLiteral(literal => {
- try
- {
- return Enum.Parse(type, (string)literal);
- }
- catch
- {
- return Enum.ToObject(type, literal);
- }
- });
- else
- return ReadLiteral(literal => {
-
- try
- {
- return Enum.Parse(type, (string)literal);
- }
- catch (Exception)
- {
- var attr = type.GetCustomAttributes(typeof(JsonUnknownAttribute), false).FirstOrDefault();
- if (attr==null)
- throw;
- return ((JsonUnknownAttribute)attr).UnknownValue;
- }
- });
- }
- // Array?
- if (type.IsArray && type.GetArrayRank() == 1)
- {
- // First parse as a List<>
- var listType = typeof(List<>).MakeGenericType(type.GetElementType());
- var list = DecoratingActivator.CreateInstance(listType);
- ParseInto(list);
- return listType.GetMethod("ToArray").Invoke(list, null);
- }
- // IEnumerable
- if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
- {
- // First parse as a List<>
- var declType = type.GetGenericArguments()[0];
- var listType = typeof(List<>).MakeGenericType(declType);
- var list = DecoratingActivator.CreateInstance(listType);
- ParseInto(list);
- return list;
- }
- // Convert interfaces to concrete types
- if (type.IsInterface)
- type = Utils.ResolveInterfaceToClass(type);
- // Untyped dictionary?
- if (_tokenizer.CurrentToken == Token.OpenBrace && (type.IsAssignableFrom(typeof(IDictionary<string, object>))))
- {
- #if !PETAJSON_NO_DYNAMIC
- var container = (new ExpandoObject()) as IDictionary<string, object>;
- #else
- var container = new Dictionary<string, object>();
- #endif
- ParseDictionary(key =>
- {
- container[key] = Parse(typeof(Object));
- });
- return container;
- }
-
- // Untyped list?
- if (_tokenizer.CurrentToken == Token.OpenSquare && (type.IsAssignableFrom(typeof(List<object>))))
- {
- var container = new List<object>();
- ParseArray(() =>
- {
- container.Add(Parse(typeof(Object)));
- });
- return container;
- }
- // Untyped literal?
- if (_tokenizer.CurrentToken == Token.Literal && type.IsAssignableFrom(_tokenizer.LiteralType))
- {
- var lit = _tokenizer.LiteralValue;
- _tokenizer.NextToken();
- return lit;
- }
- // Call value type resolver
- if (type.IsValueType)
- {
- var tp = _parsers.Get(type, () => _parserResolver(type));
- if (tp != null)
- {
- return tp(this, type);
- }
- }
- // Call reference type resolver
- if (type.IsClass && type != typeof(object))
- {
- var into = DecoratingActivator.CreateInstance(type);
- ParseInto(into);
- return into;
- }
- // Give up
- throw new InvalidDataException(string.Format("syntax error, unexpected token {0}", _tokenizer.CurrentToken));
- }
- // Parse into an existing object instance
- public void ParseInto(object into)
- {
- if (into == null)
- return;
- if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.Null)
- {
- throw new InvalidOperationException("can't parse null into existing instance");
- //return;
- }
- var type = into.GetType();
- // Existing parse into handler?
- Action<IJsonReader,object> parseInto;
- if (_intoParsers.TryGetValue(type, out parseInto))
- {
- parseInto(this, into);
- return;
- }
- // Generic dictionary?
- var dictType = Utils.FindGenericInterface(type, typeof(IDictionary<,>));
- if (dictType!=null)
- {
- // Get the key and value types
- var typeKey = dictType.GetGenericArguments()[0];
- var typeValue = dictType.GetGenericArguments()[1];
- // Parse it
- IDictionary dict = (IDictionary)into;
- dict.Clear();
- ParseDictionary(key =>
- {
- dict.Add(Convert.ChangeType(key, typeKey), Parse(typeValue));
- });
- return;
- }
- // Generic list
- var listType = Utils.FindGenericInterface(type, typeof(IList<>));
- if (listType!=null)
- {
- // Get element type
- var typeElement = listType.GetGenericArguments()[0];
- // Parse it
- IList list = (IList)into;
- list.Clear();
- ParseArray(() =>
- {
- list.Add(Parse(typeElement));
- });
- return;
- }
- // Untyped dictionary
- var objDict = into as IDictionary;
- if (objDict != null)
- {
- objDict.Clear();
- ParseDictionary(key =>
- {
- objDict[key] = Parse(typeof(Object));
- });
- return;
- }
- // Untyped list
- var objList = into as IList;
- if (objList!=null)
- {
- objList.Clear();
- ParseArray(() =>
- {
- objList.Add(Parse(typeof(Object)));
- });
- return;
- }
- // Try to resolve a parser
- var intoParser = _intoParsers.Get(type, () => _intoParserResolver(type));
- if (intoParser != null)
- {
- intoParser(this, into);
- return;
- }
- throw new InvalidOperationException(string.Format("Don't know how to parse into type '{0}'", type.FullName));
- }
- public T Parse<T>()
- {
- return (T)Parse(typeof(T));
- }
- public LiteralKind GetLiteralKind()
- {
- return _tokenizer.LiteralKind;
- }
-
- public string GetLiteralString()
- {
- return _tokenizer.String;
- }
- public void NextToken()
- {
- _tokenizer.NextToken();
- }
- // Parse a dictionary
- public void ParseDictionary(Action<string> callback)
- {
- _tokenizer.Skip(Token.OpenBrace);
- ParseDictionaryKeys(key => { callback(key); return true; });
- _tokenizer.Skip(Token.CloseBrace);
- }
- // Parse dictionary keys, calling callback for each one. Continues until end of input
- // or when callback returns false
- private void ParseDictionaryKeys(Func<string, bool> callback)
- {
- // End?
- while (_tokenizer.CurrentToken != Token.CloseBrace)
- {
- // Parse the key
- string key = null;
- if (_tokenizer.CurrentToken == Token.Identifier && (_options & JsonOptions.StrictParser)==0)
- {
- key = _tokenizer.String;
- }
- else if (_tokenizer.CurrentToken == Token.Literal && _tokenizer.LiteralKind == LiteralKind.String)
- {
- key = (string)_tokenizer.LiteralValue;
- }
- else
- {
- throw new InvalidDataException("syntax error, expected string literal or identifier");
- }
- _tokenizer.NextToken();
- _tokenizer.Skip(Token.Colon);
- // Remember current position
- var pos = _tokenizer.CurrentTokenPosition;
- // Call the callback, quit if cancelled
- _contextStack.Add(key);
- bool doDefaultProcessing = callback(key);
- _contextStack.RemoveAt(_contextStack.Count-1);
- if (!doDefaultProcessing)
- return;
- // If the callback didn't read anything from the tokenizer, then skip it ourself
- if (pos.Line == _tokenizer.CurrentTokenPosition.Line && pos.Offset == _tokenizer.CurrentTokenPosition.Offset)
- {
- Parse(typeof(object));
- }
- // Separating/trailing comma
- if (_tokenizer.SkipIf(Token.Comma))
- {
- if ((_options & JsonOptions.StrictParser) != 0 && _tokenizer.CurrentToken == Token.CloseBrace)
- {
- throw new InvalidDataException("Trailing commas not allowed in strict mode");
- }
- continue;
- }
- // End
- break;
- }
- }
- // Parse an array
- public void ParseArray(Action callback)
- {
- _tokenizer.Skip(Token.OpenSquare);
- int index = 0;
- while (_tokenizer.CurrentToken != Token.CloseSquare)
- {
- _contextStack.Add(string.Format("[{0}]", index));
- callback();
- _contextStack.RemoveAt(_contextStack.Count-1);
- if (_tokenizer.SkipIf(Token.Comma))
- {
- if ((_options & JsonOptions.StrictParser)!=0 && _tokenizer.CurrentToken==Token.CloseSquare)
- {
- throw new InvalidDataException("Trailing commas not allowed in strict mode");
- }
- continue;
- }
- break;
- }
- _tokenizer.Skip(Token.CloseSquare);
- }
- // Yikes!
- public static Func<Type, Action<IJsonReader, object>> _intoParserResolver;
- public static Func<Type, Func<IJsonReader, Type, object>> _parserResolver;
- public static ThreadSafeCache<Type, Func<IJsonReader, Type, object>> _parsers = new ThreadSafeCache<Type, Func<IJsonReader, Type, object>>();
- public static ThreadSafeCache<Type, Action<IJsonReader, object>> _intoParsers = new ThreadSafeCache<Type, Action<IJsonReader, object>>();
- public static ThreadSafeCache<Type, Func<IJsonReader, string, object>> _typeFactories = new ThreadSafeCache<Type, Func<IJsonReader, string, object>>();
- }
- public class Writer : IJsonWriter
- {
- static Writer()
- {
- _formatterResolver = ResolveFormatter;
- // Register standard formatters
- _formatters.Add(typeof(string), (w, o) => w.WriteStringLiteral((string)o));
- _formatters.Add(typeof(char), (w, o) => w.WriteStringLiteral(((char)o).ToString()));
- _formatters.Add(typeof(bool), (w, o) => w.WriteRaw(((bool)o) ? "true" : "false"));
- Action<IJsonWriter, object> convertWriter = (w, o) => w.WriteRaw((string)Convert.ChangeType(o, typeof(string), System.Globalization.CultureInfo.InvariantCulture));
- _formatters.Add(typeof(int), convertWriter);
- _formatters.Add(typeof(uint), convertWriter);
- _formatters.Add(typeof(long), convertWriter);
- _formatters.Add(typeof(ulong), convertWriter);
- _formatters.Add(typeof(short), convertWriter);
- _formatters.Add(typeof(ushort), convertWriter);
- _formatters.Add(typeof(decimal), convertWriter);
- _formatters.Add(typeof(byte), convertWriter);
- _formatters.Add(typeof(sbyte), convertWriter);
- _formatters.Add(typeof(DateTime), (w, o) => convertWriter(w, Utils.ToUnixMilliseconds((DateTime)o)));
- _formatters.Add(typeof(float), (w, o) => w.WriteRaw(((float)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
- _formatters.Add(typeof(double), (w, o) => w.WriteRaw(((double)o).ToString("R", System.Globalization.CultureInfo.InvariantCulture)));
- _formatters.Add(typeof(byte[]), (w, o) =>
- {
- w.WriteRaw("\"");
- w.WriteRaw(Convert.ToBase64String((byte[])o));
- w.WriteRaw("\"");
- });
- }
- public static Func<Type, Action<IJsonWriter, object>> _formatterResolver;
- public static Dictionary<Type, Action<IJsonWriter, object>> _formatters = new Dictionary<Type, Action<IJsonWriter, object>>();
- static Action<IJsonWriter, object> ResolveFormatter(Type type)
- {
- // Try `void FormatJson(IJsonWriter)`
- var formatJson = ReflectionInfo.FindFormatJson(type);
- if (formatJson != null)
- {
- if (formatJson.ReturnType==typeof(void))
- return (w, obj) => formatJson.Invoke(obj, new Object[] { w });
- if (formatJson.ReturnType == typeof(string))
- return (w, obj) => w.WriteStringLiteral((string)formatJson.Invoke(obj, new Object[] { }));
- }
- var ri = ReflectionInfo.GetReflectionInfo(type);
- if (ri != null)
- return ri.Write;
- else
- return null;
- }
- public Writer(TextWriter w, JsonOptions options)
- {
- _writer = w;
- _atStartOfLine = true;
- _needElementSeparator = false;
- _options = options;
- }
- private TextWriter _writer;
- private int IndentLevel;
- private bool _atStartOfLine;
- private bool _needElementSeparator = false;
- private JsonOptions _options;
- private char _currentBlockKind = '\0';
- // Move to the next line
- public void NextLine()
- {
- if (_atStartOfLine)
- return;
- if ((_options & JsonOptions.WriteWhitespace)!=0)
- {
- WriteRaw("\n");
- WriteRaw(new string('\t', IndentLevel));
- }
- _atStartOfLine = true;
- }
- // Start the next element, writing separators and white space
- void NextElement()
- {
- if (_needElementSeparator)
- {
- WriteRaw(",");
- NextLine();
- }
- else
- {
- NextLine();
- IndentLevel++;
- WriteRaw(_currentBlockKind.ToString());
- NextLine();
- }
- _needElementSeparator = true;
- }
- // Write next array element
- public void WriteElement()
- {
- if (_currentBlockKind != '[')
- throw new InvalidOperationException("Attempt to write array element when not in array block");
- NextElement();
- }
- // Write next dictionary key
- public void WriteKey(string key)
- {
- if (_currentBlockKind != '{')
- throw new InvalidOperationException("Attempt to write dictionary element when not in dictionary block");
- NextElement();
- WriteStringLiteral(key);
- WriteRaw(((_options & JsonOptions.WriteWhitespace) != 0) ? ": " : ":");
- }
- // Write an already escaped dictionary key
- public void WriteKeyNoEscaping(string key)
- {
- if (_currentBlockKind != '{')
- throw new InvalidOperationException("Attempt to write dictionary element when not in dictionary block");
- NextElement();
- WriteRaw("\"");
- WriteRaw(key);
- WriteRaw("\"");
- WriteRaw(((_options & JsonOptions.WriteWhitespace) != 0) ? ": " : ":");
- }
- // Write anything
- public void WriteRaw(string str)
- {
- _atStartOfLine = false;
- _writer.Write(str);
- }
- static int IndexOfEscapeableChar(string str, int pos)
- {
- int length = str.Length;
- while (pos < length)
- {
- var ch = str[pos];
- if (ch == '\\' || ch == '/' || ch == '\"' || (ch>=0 && ch <= 0x1f) || (ch >= 0x7f && ch <=0x9f) || ch==0x2028 || ch== 0x2029)
- return pos;
- pos++;
- }
- return -1;
- }
- public void WriteStringLiteral(string str)
- {
- _atStartOfLine = false;
- if (str == null)
- {
- _writer.Write("null");
- return;
- }
- _writer.Write("\"");
- int pos = 0;
- int escapePos;
- while ((escapePos = IndexOfEscapeableChar(str, pos)) >= 0)
- {
- if (escapePos > pos)
- _writer.Write(str.Substring(pos, escapePos - pos));
- switch (str[escapePos])
- {
- case '\"': _writer.Write("\\\""); break;
- case '\\': _writer.Write("\\\\"); break;
- case '/': _writer.Write("\\/"); break;
- case '\b': _writer.Write("\\b"); break;
- case '\f': _writer.Write("\\f"); break;
- case '\n': _writer.Write("\\n"); break;
- case '\r': _writer.Write("\\r"); break;
- case '\t': _writer.Write("\\t"); break;
- default:
- _writer.Write(string.Format("\\u{0:x4}", (int)str[escapePos]));
- break;
- }
- pos = escapePos + 1;
- }
- if (str.Length > pos)
- _writer.Write(str.Substring(pos));
- _writer.Write("\"");
- }
- // Write an array or dictionary block
- private void WriteBlock(string open, string close, Action callback)
- {
- var prevBlockKind = _currentBlockKind;
- _currentBlockKind = open[0];
- var didNeedElementSeparator = _needElementSeparator;
- _needElementSeparator = false;
- callback();
- if (_needElementSeparator)
- {
- IndentLevel--;
- NextLine();
- }
- else
- {
- WriteRaw(open);
- }
- WriteRaw(close);
- _needElementSeparator = didNeedElementSeparator;
- _currentBlockKind = prevBlockKind;
- }
- // Write an array
- public void WriteArray(Action callback)
- {
- WriteBlock("[", "]", callback);
- }
- // Write a dictionary
- public void WriteDictionary(Action callback)
- {
- WriteBlock("{", "}", callback);
- }
- // Write any value
- public void WriteValue(object value)
- {
- _atStartOfLine = false;
- // Special handling for null
- if (value == null)
- {
- _writer.Write("null");
- return;
- }
- var type = value.GetType();
- // Handle nullable types
- var typeUnderlying = Nullable.GetUnderlyingType(type);
- if (typeUnderlying != null)
- type = typeUnderlying;
- // Look up type writer
- Action<IJsonWriter, object> typeWriter;
- if (_formatters.TryGetValue(type, out typeWriter))
- {
- // Write it
- typeWriter(this, value);
- return;
- }
- // Enumerated type?
- if (type.IsEnum)
- {
- if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
- WriteRaw(Convert.ToUInt32(value).ToString(CultureInfo.InvariantCulture));
- else
- WriteStringLiteral(value.ToString());
- return;
- }
- // Dictionary?
- var d = value as System.Collections.IDictionary;
- if (d != null)
- {
- WriteDictionary(() =>
- {
- foreach (var key in d.Keys)
- {
- WriteKey(key.ToString());
- WriteValue(d[key]);
- }
- });
- return;
- }
- // Dictionary?
- var dso = value as IDictionary<string,object>;
- if (dso != null)
- {
- WriteDictionary(() =>
- {
- foreach (var key in dso.Keys)
- {
- WriteKey(key.ToString());
- WriteValue(dso[key]);
- }
- });
- return;
- }
- // Array?
- var e = value as System.Collections.IEnumerable;
- if (e != null)
- {
- WriteArray(() =>
- {
- foreach (var i in e)
- {
- WriteElement();
- WriteValue(i);
- }
- });
- return;
- }
- // Resolve a formatter
- var formatter = _formatterResolver(type);
- if (formatter != null)
- {
- _formatters[type] = formatter;
- formatter(this, value);
- return;
- }
- // Give up
- throw new InvalidDataException(string.Format("Don't know how to write '{0}' to json", value.GetType()));
- }
- }
- // Information about a field or property found through reflection
- public class JsonMemberInfo
- {
- // The Json key for this member
- public string JsonKey;
- // True if should keep existing instance (reference types only)
- public bool KeepInstance;
- // True if deprecated
- public bool Deprecated;
-
- // Reflected member info
- MemberInfo _mi;
- public MemberInfo Member
- {
- get { return _mi; }
- set
- {
- // Store it
- _mi = value;
- // Also create getters and setters
- if (_mi is PropertyInfo)
- {
- GetValue = (obj) => ((PropertyInfo)_mi).GetValue(obj, null);
- SetValue = (obj, val) => ((PropertyInfo)_mi).SetValue(obj, val, null);
- }
- else
- {
- GetValue = ((FieldInfo)_mi).GetValue;
- SetValue = ((FieldInfo)_mi).SetValue;
- }
- }
- }
- // Member type
- public Type MemberType
- {
- get
- {
- if (Member is PropertyInfo)
- {
- return ((PropertyInfo)Member).PropertyType;
- }
- else
- {
- return ((FieldInfo)Member).FieldType;
- }
- }
- }
- // Get/set helpers
- public Action<object, object> SetValue;
- public Func<object, object> GetValue;
- }
- // Stores reflection info about a type
- public class ReflectionInfo
- {
- // List of members to be serialized
- public List<JsonMemberInfo> Members;
- // Cache of these ReflectionInfos's
- static ThreadSafeCache<Type, ReflectionInfo> _cache = new ThreadSafeCache<Type, ReflectionInfo>();
- public static MethodInfo FindFormatJson(Type type)
- {
- if (type.IsValueType)
- {
- // Try `void FormatJson(IJsonWriter)`
- var formatJson = type.GetMethod("FormatJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(IJsonWriter) }, null);
- if (formatJson != null && formatJson.ReturnType == typeof(void))
- return formatJson;
- // Try `string FormatJson()`
- formatJson = type.GetMethod("FormatJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);
- if (formatJson != null && formatJson.ReturnType == typeof(string))
- return formatJson;
- }
- return null;
- }
- public static MethodInfo FindParseJson(Type type)
- {
- // Try `T ParseJson(IJsonReader)`
- var parseJson = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(IJsonReader) }, null);
- if (parseJson != null && parseJson.ReturnType == type)
- return parseJson;
- // Try `T ParseJson(string)`
- parseJson = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
- if (parseJson != null && parseJson.ReturnType == type)
- return parseJson;
- return null;
- }
- // Write one of these types
- public void Write(IJsonWriter w, object val)
- {
- w.WriteDictionary(() =>
- {
- var writing = val as IJsonWriting;
- if (writing != null)
- writing.OnJsonWriting(w);
- foreach (var jmi in Members.Where(x=>!x.Deprecated))
- {
- w.WriteKeyNoEscaping(jmi.JsonKey);
- w.WriteValue(jmi.GetValue(val));
- }
- var written = val as IJsonWritten;
- if (written != null)
- written.OnJsonWritten(w);
- });
- }
- // Read one of these types.
- // NB: Although PetaJson.JsonParseInto only works on reference type, when using reflection
- // it also works for value types so we use the one method for both
- public void ParseInto(IJsonReader r, object into)
- {
- var loading = into as IJsonLoading;
- if (loading != null)
- loading.OnJsonLoading(r);
- r.ParseDictionary(key =>
- {
- ParseFieldOrProperty(r, into, key);
- });
- var loaded = into as IJsonLoaded;
- if (loaded != null)
- loaded.OnJsonLoaded(r);
- }
- // The member info is stored in a list (as opposed to a dictionary) so that
- // the json is written in the same order as the fields/properties are defined
- // On loading, we assume the fields will be in the same order, but need to
- // handle if they're not. This function performs a linear search, but
- // starts after the last found item as an optimization that should work
- // most of the time.
- int _lastFoundIndex = 0;
- bool FindMemberInfo(string name, out JsonMemberInfo found)
- {
- for (int i = 0; i < Members.Count; i++)
- {
- int index = (i + _lastFoundIndex) % Members.Count;
- var jmi = Members[index];
- if (jmi.JsonKey == name)
- {
- _lastFoundIndex = index;
- found = jmi;
- return true;
- }
- }
- found = null;
- return false;
- }
- // Parse a value from IJsonReader into an object instance
- public void ParseFieldOrProperty(IJsonReader r, object into, string key)
- {
- // IJsonLoadField
- var lf = into as IJsonLoadField;
- if (lf != null && lf.OnJsonField(r, key))
- return;
- // Find member
- JsonMemberInfo jmi;
- if (FindMemberInfo(key, out jmi))
- {
- // Try to keep existing instance
- if (jmi.KeepInstance)
- {
- var subInto = jmi.GetValue(into);
- if (subInto != null)
- {
- r.ParseInto(subInto);
- return;
- }
- }
- // Parse and set
- var val = r.Parse(jmi.MemberType);
- jmi.SetValue(into, val);
- return;
- }
- }
- // Get the reflection info for a specified type
- public static ReflectionInfo GetReflectionInfo(Type type)
- {
- // Check cache
- return _cache.Get(type, () =>
- {
- var allMembers = Utils.GetAllFieldsAndProperties(type);
- // Does type have a [Json] attribute
- bool typeMarked = type.GetCustomAttributes(typeof(JsonAttribute), true).OfType<JsonAttribute>().Any();
- // Do any members have a [Json] attribute
- bool anyFieldsMarked = allMembers.Any(x => x.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().Any());
- #if !PETAJSON_NO_DATACONTRACT
- // Try with DataContract and friends
- if (!typeMarked && !anyFieldsMarked && type.GetCustomAttributes(typeof(DataContractAttribute), true).OfType<DataContractAttribute>().Any())
- {
- var ri = CreateReflectionInfo(type, mi =>
- {
- // Get attributes
- var attr = mi.GetCustomAttributes(typeof(DataMemberAttribute), false).OfType<DataMemberAttribute>().FirstOrDefault();
- if (attr != null)
- {
- return new JsonMemberInfo()
- {
- Member = mi,
- JsonKey = attr.Name ?? mi.Name, // No lower case first letter if using DataContract/Member
- };
- }
- return null;
- });
- ri.Members.Sort((a, b) => String.CompareOrdinal(a.JsonKey, b.JsonKey)); // Match DataContractJsonSerializer
- return ri;
- }
- #endif
- {
- // Should we serialize all public methods?
- bool serializeAllPublics = typeMarked || !anyFieldsMarked;
- // Build
- var ri = CreateReflectionInfo(type, mi =>
- {
- // Explicitly excluded?
- if (mi.GetCustomAttributes(typeof(JsonExcludeAttribute), false).Any())
- return null;
- // Get attributes
- var attr = mi.GetCustomAttributes(typeof(JsonAttribute), false).OfType<JsonAttribute>().FirstOrDefault();
- if (attr != null)
- {
- return new JsonMemberInfo()
- {
- Member = mi,
- JsonKey = attr.Key ?? mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
- KeepInstance = attr.KeepInstance,
- Deprecated = attr.Deprecated,
- };
- }
- // Serialize all publics?
- if (serializeAllPublics && Utils.IsPublic(mi))
- {
- return new JsonMemberInfo()
- {
- Member = mi,
- JsonKey = mi.Name.Substring(0, 1).ToLower() + mi.Name.Substring(1),
- };
- }
- return null;
- });
- return ri;
- }
- });
- }
- public static ReflectionInfo CreateReflectionInfo(Type type, Func<MemberInfo, JsonMemberInfo> callback)
- {
- // Work out properties and fields
- var members = Utils.GetAllFieldsAndProperties(type).Select(x => callback(x)).Where(x => x != null).ToList();
- // Anything with KeepInstance must be a reference type
- var invalid = members.FirstOrDefault(x => x.KeepInstance && x.MemberType.IsValueType);
- if (invalid!=null)
- {
- throw new InvalidOperationException(string.Format("KeepInstance=true can only be applied to reference types ({0}.{1})", type.FullName, invalid.Member));
- }
- // Must have some members
- if (!members.Any() && !Attribute.IsDefined(type, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), false))
- return null;
- // Create reflection info
- return new ReflectionInfo() { Members = members };
- }
- }
- public class ThreadSafeCache<TKey, TValue>
- {
- public ThreadSafeCache()
- {
- }
- public TValue Get(TKey key, Func<TValue> createIt)
- {
- // Check if already exists
- _lock.EnterReadLock();
- try
- {
- TValue val;
- if (_map.TryGetValue(key, out val))
- return val;
- }
- finally
- {
- _lock.ExitReadLock();
- }
- // Nope, take lock and try again
- _lock.EnterWriteLock();
- try
- {
- // Check again before creating it
- TValue val;
- if (!_map.TryGetValue(key, out val))
- {
- // Store the new one
- val = createIt();
- _map[key] = val;
- }
- return val;
- }
- finally
- {
- _lock.ExitWriteLock();
- }
- }
- public bool TryGetValue(TKey key, out TValue val)
- {
- _lock.EnterReadLock();
- try
- {
- return _map.TryGetValue(key, out val);
- }
- finally
- {
- _lock.ExitReadLock();
- }
- }
- public void Set(TKey key, TValue value)
- {
- _lock.EnterWriteLock();
- try
- {
- _map[key] = value;
- }
- finally
- {
- _lock.ExitWriteLock();
- }
- }
- Dictionary<TKey, TValue> _map = new Dictionary<TKey,TValue>();
- ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
- }
- internal static class Utils
- {
- // Get all fields and properties of a type
- public static IEnumerable<MemberInfo> GetAllFieldsAndProperties(Type t)
- {
- if (t == null)
- return Enumerable.Empty<FieldInfo>();
- BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
- return t.GetMembers(flags).Where(x => x is FieldInfo || x is PropertyInfo).Concat(GetAllFieldsAndProperties(t.BaseType));
- }
- public static Type FindGenericInterface(Type type, Type tItf)
- {
- foreach (var t in type.GetInterfaces())
- {
- // Is this a generic list?
- if (t.IsGenericType && t.GetGenericTypeDefinition() == tItf)
- return t;
- }
- return null;
- }
- public static bool IsPublic(MemberInfo mi)
- {
- // Public field
- var fi = mi as FieldInfo;
- if (fi != null)
- return fi.IsPublic;
- // Public property
- // (We only check the get method so we can work with anonymous types)
- var pi = mi as PropertyInfo;
- if (pi != null)
- {
- var gm = pi.GetGetMethod(true);
- return (gm != null && gm.IsPublic);
- }
- return false;
- }
- public static Type ResolveInterfaceToClass(Type tItf)
- {
- // Generic type
- if (tItf.IsGenericType)
- {
- var genDef = tItf.GetGenericTypeDefinition();
- // IList<> -> List<>
- if (genDef == typeof(IList<>))
- {
- return typeof(List<>).MakeGenericType(tItf.GetGenericArguments());
- }
- // IDictionary<string,> -> Dictionary<string,>
- if (genDef == typeof(IDictionary<,>) && tItf.GetGenericArguments()[0] == typeof(string))
- {
- return typeof(Dictionary<,>).MakeGenericType(tItf.GetGenericArguments());
- }
- }
- // IEnumerable -> List<object>
- if (tItf == typeof(IEnumerable))
- return typeof(List<object>);
- // IDicitonary -> Dictionary<string,object>
- if (tItf == typeof(IDictionary))
- return typeof(Dictionary<string, object>);
- return tItf;
- }
- public static long ToUnixMilliseconds(DateTime This)
- {
- return (long)This.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
- }
- public static DateTime FromUnixMilliseconds(long timeStamp)
- {
- return new DateTime(1970, 1, 1).AddMilliseconds(timeStamp);
- }
- }
- public class Tokenizer
- {
- public Tokenizer(TextReader r, JsonOptions options)
- {
- _underlying = r;
- _options = options;
- FillBuffer();
- NextChar();
- NextToken();
- }
- private JsonOptions _options;
- private StringBuilder _sb = new StringBuilder();
- private TextReader _underlying;
- private char[] _buf = new char[4096];
- private int _pos;
- private int _bufUsed;
- private StringBuilder _rewindBuffer;
- private int _rewindBufferPos;
- private JsonLineOffset _currentCharPos;
- private char _currentChar;
- private Stack<ReaderState> _bookmarks = new Stack<ReaderState>();
- public JsonLineOffset CurrentTokenPosition;
- public Token CurrentToken;
- public LiteralKind LiteralKind;
- public string String;
- public object LiteralValue
- {
- get
- {
- if (CurrentToken != Token.Literal)
- throw new InvalidOperationException("token is not a literal");
- switch (LiteralKind)
- {
- case LiteralKind.Null: return null;
- case LiteralKind.False: return false;
- case LiteralKind.True: return true;
- case LiteralKind.String: return String;
- case LiteralKind.SignedInteger: return long.Parse(String, CultureInfo.InvariantCulture);
- case LiteralKind.UnsignedInteger:
- if (String.StartsWith("0x") || String.StartsWith("0X"))
- return Convert.ToUInt64(String.Substring(2), 16);
- else
- return ulong.Parse(String, CultureInfo.InvariantCulture);
- case LiteralKind.FloatingPoint: return double.Parse(String, CultureInfo.InvariantCulture);
- }
- return null;
- }
- }
- public Type LiteralType
- {
- get
- {
- if (CurrentToken != Token.Literal)
- throw new InvalidOperationException("token is not a literal");
- switch (LiteralKind)
- {
- case LiteralKind.Null: return typeof(Object);
- case LiteralKind.False: return typeof(Boolean);
- case LiteralKind.True: return typeof(Boolean);
- case LiteralKind.String: return typeof(string);
- case LiteralKind.SignedInteger: return typeof(long);
- case LiteralKind.UnsignedInteger: return typeof(ulong);
- case LiteralKind.FloatingPoint: return typeof(double);
- }
- return null;
- }
- }
- // This object represents the entire state of the reader and is used for rewind
- struct ReaderState
- {
- public ReaderState(Tokenizer tokenizer)
- {
- _currentCharPos = tokenizer._currentCharPos;
- _currentChar = tokenizer._currentChar;
- _string = tokenizer.String;
- _literalKind = tokenizer.LiteralKind;
- _rewindBufferPos = tokenizer._rewindBufferPos;
- _currentTokenPos = tokenizer.CurrentTokenPosition;
- _currentToken = tokenizer.CurrentToken;
- }
- public void Apply(Tokenizer tokenizer)
- {
- tokenizer._currentCharPos = _currentCharPos;
- tokenizer._currentChar = _currentChar;
- tokenizer._rewindBufferPos = _rewindBufferPos;
- tokenizer.CurrentToken = _currentToken;
- tokenizer.CurrentTokenPosition = _currentTokenPos;
- tokenizer.String = _string;
- tokenizer.LiteralKind = _literalKind;
- }
- private JsonLineOffset _currentCharPos;
- private JsonLineOffset _currentTokenPos;
- private char _currentChar;
- private Token _currentToken;
- private LiteralKind _literalKind;
- private string _string;
- private int _rewindBufferPos;
- }
- // Create a rewind bookmark
- public void CreateBookmark()
- {
- _bookmarks.Push(new ReaderState(this));
- if (_rewindBuffer == null)
- {
- _rewindBuffer = new StringBuilder();
- _rewindBufferPos = 0;
- }
- }
- // Discard bookmark
- public void DiscardBookmark()
- {
- _bookmarks.Pop();
- if (_bookmarks.Count == 0)
- {
- _rewindBuffer = null;
- _rewindBufferPos = 0;
- }
- }
- // Rewind to a bookmark
- public void RewindToBookmark()
- {
- _bookmarks.Pop().Apply(this);
- }
- // Fill buffer by reading from underlying TextReader
- void FillBuffer()
- {
- _bufUsed = _underlying.Read(_buf, 0, _buf.Length);
- _pos = 0;
- }
- // Get the next character from the input stream
- // (this function could be extracted into a few different methods, but is mostly inlined
- // for performance - yes it makes a difference)
- public char NextChar()
- {
- if (_rewindBuffer == null)
- {
- if (_pos >= _bufUsed)
- {
- if (_bufUsed > 0)
- {
- FillBuffer();
- }
- if (_bufUsed == 0)
- {
- return _currentChar = '\0';
- }
- }
- // Next
- _currentCharPos.Offset++;
- return _currentChar = _buf[_pos++];
- }
- if (_rewindBufferPos < _rewindBuffer.Length)
- {
- _currentCharPos.Offset++;
- return _currentChar = _rewindBuffer[_rewindBufferPos++];
- }
- else
- {
- if (_pos >= _bufUsed && _bufUsed > 0)
- FillBuffer();
- _currentChar = _bufUsed == 0 ? '\0' : _buf[_pos++];
- _rewindBuffer.Append(_currentChar);
- _rewindBufferPos++;
- _currentCharPos.Offset++;
- return _currentChar;
- }
- }
- // Read the next token from the input stream
- // (Mostly inline for performance)
- public void NextToken()
- {
- while (true)
- {
- // Skip whitespace and handle line numbers
- while (true)
- {
- if (_currentChar == '\r')
- {
- if (NextChar() == '\n')
- {
- NextChar();
- }
- _currentCharPos.Line++;
- _currentCharPos.Offset = 0;
- }
- else if (_currentChar == '\n')
- {
- if (NextChar() == '\r')
- {
- NextChar();
- }
- _currentCharPos.Line++;
- _currentCharPos.Offset = 0;
- }
- else if (_currentChar == ' ')
- {
- NextChar();
- }
- else if (_currentChar == '\t')
- {
- NextChar();
- }
- else
- break;
- }
-
- // Remember position of token
- CurrentTokenPosition = _currentCharPos;
- // Handle common characters first
- switch (_currentChar)
- {
- case '/':
- // Comments not support in strict mode
- if ((_options & JsonOptions.StrictParser) != 0)
- {
- throw new InvalidDataException(string.Format("syntax error, unexpected character '{0}'", _currentChar));
- }
- // Process comment
- NextChar();
- switch (_currentChar)
- {
- case '/':
- NextChar();
- while (_currentChar!='\0' && _currentChar != '\r' && _currentChar != '\n')
- {
- NextChar();
- }
- break;
- case '*':
- bool endFound = false;
- while (!endFound && _currentChar!='\0')
- {
- if (_currentChar == '*')
- {
- NextChar();
- if (_currentChar == '/')
- {
- endFound = true;
- }
- }
- NextChar();
- }
- break;
- default:
- throw new InvalidDataException("syntax error, unexpected character after slash");
- }
- continue;
- case '\"':
- case '\'':
- {
- _sb.Length = 0;
- var quoteKind = _currentChar;
- NextChar();
- while (_currentChar!='\0')
- {
- if (_currentChar == '\\')
- {
- NextChar();
- var escape = _currentChar;
- switch (escape)
- {
- case '\"': _sb.Append('\"'); break;
- case '\\': _sb.Append('\\'); break;
- case '/': _sb.Append('/'); break;
- case 'b': _sb.Append('\b'); break;
- case 'f': _sb.Append('\f'); break;
- case 'n': _sb.Append('\n'); break;
- case 'r': _sb.Append('\r'); break;
- case 't': _sb.Append('\t'); break;
- case 'u':
- var sbHex = new StringBuilder();
- for (int i = 0; i < 4; i++)
- {
- NextChar();
- sbHex.Append(_currentChar);
- }
- _sb.Append((char)Convert.ToUInt16(sbHex.ToString(), 16));
- break;
- default:
- throw new InvalidDataException(string.Format("Invalid escape sequence in string literal: '\\{0}'", _currentChar));
- }
- }
- else if (_currentChar == quoteKind)
- {
- String = _sb.ToString();
- CurrentToken = Token.Literal;
- LiteralKind = LiteralKind.String;
- NextChar();
- return;
- }
- else
- {
- _sb.Append(_currentChar);
- }
- NextChar();
- }
- throw new InvalidDataException("syntax error, unterminated string literal");
- }
- case '{': CurrentToken = Token.OpenBrace; NextChar(); return;
- case '}': CurrentToken = Token.CloseBrace; NextChar(); return;
- case '[': CurrentToken = Token.OpenSquare; NextChar(); return;
- case ']': CurrentToken = Token.CloseSquare; NextChar(); return;
- case '=': CurrentToken = Token.Equal; NextChar(); return;
- case ':': CurrentToken = Token.Colon; NextChar(); return;
- case ';': CurrentToken = Token.SemiColon; NextChar(); return;
- case ',': CurrentToken = Token.Comma; NextChar(); return;
- case '\0': CurrentToken = Token.EOF; return;
- }
- // Number?
- if (char.IsDigit(_currentChar) || _currentChar == '-')
- {
- TokenizeNumber();
- return;
- }
- // Identifier? (checked for after everything else as identifiers are actually quite rare in valid json)
- if (Char.IsLetter(_currentChar) || _currentChar == '_' || _currentChar == '$')
- {
- // Find end of identifier
- _sb.Length = 0;
- while (Char.IsLetterOrDigit(_currentChar) || _currentChar == '_' || _currentChar == '$')
- {
- _sb.Append(_currentChar);
- NextChar();
- }
- String = _sb.ToString();
- // Handle special identifiers
- switch (String)
- {
- case "true":
- LiteralKind = LiteralKind.True;
- CurrentToken = Token.Literal;
- return;
- case "false":
- LiteralKind = LiteralKind.False;
- CurrentToken = Token.Literal;
- return;
- case "null":
- LiteralKind = LiteralKind.Null;
- CurrentToken = Token.Literal;
- return;
- }
- CurrentToken = Token.Identifier;
- return;
- }
- // What the?
- throw new InvalidDataException(string.Format("syntax error, unexpected character '{0}'", _currentChar));
- }
- }
- // Parse a sequence of characters that could make up a valid number
- // For performance, we don't actually parse it into a number yet. When using PetaJsonEmit we parse
- // later, directly into a value type to avoid boxing
- private void TokenizeNumber()
- {
- _sb.Length = 0;
- // Leading negative sign
- bool signed = false;
- if (_currentChar == '-')
- {
- signed = true;
- _sb.Append(_currentChar);
- NextChar();
- }
- // Hex prefix?
- bool hex = false;
- if (_currentChar == '0' && (_options & JsonOptions.StrictParser)==0)
- {
- _sb.Append(_currentChar);
- NextChar();
- if (_currentChar == 'x' || _currentChar == 'X')
- {
- _sb.Append(_currentChar);
- NextChar();
- hex = true;
- }
- }
- // Process characters, but vaguely figure out what type it is
- bool cont = true;
- bool fp = false;
- while (cont)
- {
- switch (_currentChar)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- _sb.Append(_currentChar);
- NextChar();
- break;
- case 'A':
- case 'a':
- case 'B':
- case 'b':
- case 'C':
- case 'c':
- case 'D':
- case 'd':
- case 'F':
- case 'f':
- if (!hex)
- cont = false;
- else
- {
- _sb.Append(_currentChar);
- NextChar();
- }
- break;
- case '.':
- if (hex)
- {
- cont = false;
- }
- else
- {
- fp = true;
- _sb.Append(_currentChar);
- NextChar();
- }
- break;
- case 'E':
- case 'e':
- if (!hex)
- {
- fp = true;
- _sb.Append(_currentChar);
- NextChar();
- if (_currentChar == '+' || _currentChar == '-')
- {
- _sb.Append(_currentChar);
- NextChar();
- }
- }
- break;
- default:
- cont = false;
- break;
- }
- }
- if (char.IsLetter(_currentChar))
- throw new InvalidDataException(string.Format("syntax error, invalid character following number '{0}'", _sb.ToString()));
- // Setup token
- String = _sb.ToString();
- CurrentToken = Token.Literal;
- // Setup literal kind
- if (fp)
- {
- LiteralKind = LiteralKind.FloatingPoint;
- }
- else if (signed)
- {
- LiteralKind = LiteralKind.SignedInteger;
- }
- else
- {
- LiteralKind = LiteralKind.UnsignedInteger;
- }
- }
- // Check the current token, throw exception if mismatch
- public void Check(Token tokenRequired)
- {
- if (tokenRequired != CurrentToken)
- {
- throw new InvalidDataException(string.Format("syntax error, expected {0} found {1}", tokenRequired, CurrentToken));
- }
- }
- // Skip token which must match
- public void Skip(Token tokenRequired)
- {
- Check(tokenRequired);
- NextToken();
- }
- // Skip token if it matches
- public bool SkipIf(Token tokenRequired)
- {
- if (tokenRequired == CurrentToken)
- {
- NextToken();
- return true;
- }
- return false;
- }
- }
- #if !PETAJSON_NO_EMIT
- static class Emit
- {
- // Generates a function that when passed an object of specified type, renders it to an IJsonReader
- public static Action<IJsonWriter, object> MakeFormatter(Type type)
- {
- var formatJson = ReflectionInfo.FindFormatJson(type);
- if (formatJson != null)
- {
- var method = new DynamicMethod("invoke_formatJson", null, new Type[] { typeof(IJsonWriter), typeof(Object) }, true);
- var il = method.GetILGenerator();
- if (formatJson.ReturnType == typeof(string))
- {
- // w.WriteStringLiteral(o.FormatJson())
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Unbox, type);
- il.Emit(OpCodes.Call, formatJson);
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral"));
- }
- else
- {
- // o.FormatJson(w);
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(type.IsValueType ? OpCodes.Unbox : OpCodes.Castclass, type);
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, formatJson);
- }
- il.Emit(OpCodes.Ret);
- return (Action<IJsonWriter, object>)method.CreateDelegate(typeof(Action<IJsonWriter, object>));
- }
- else
- {
- // Get the reflection info for this type
- var ri = ReflectionInfo.GetReflectionInfo(type);
- if (ri == null)
- return null;
- // Create a dynamic method that can do the work
- var method = new DynamicMethod("dynamic_formatter", null, new Type[] { typeof(IJsonWriter), typeof(object) }, true);
- var il = method.GetILGenerator();
- // Cast/unbox the target object and store in local variable
- var locTypedObj = il.DeclareLocal(type);
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
- il.Emit(OpCodes.Stloc, locTypedObj);
- // Get Invariant CultureInfo (since we'll probably be needing this)
- var locInvariant = il.DeclareLocal(typeof(IFormatProvider));
- il.Emit(OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod());
- il.Emit(OpCodes.Stloc, locInvariant);
- // These are the types we'll call .ToString(Culture.InvariantCulture) on
- var toStringTypes = new Type[] {
- typeof(int), typeof(uint), typeof(long), typeof(ulong),
- typeof(short), typeof(ushort), typeof(decimal),
- typeof(byte), typeof(sbyte)
- };
- // Theses types we also generate for
- var otherSupportedTypes = new Type[] {
- typeof(double), typeof(float), typeof(string), typeof(char)
- };
- // Call IJsonWriting if implemented
- if (typeof(IJsonWriting).IsAssignableFrom(type))
- {
- if (type.IsValueType)
- {
- il.Emit(OpCodes.Ldloca, locTypedObj);
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, type.GetInterfaceMap(typeof(IJsonWriting)).TargetMethods[0]);
- }
- else
- {
- il.Emit(OpCodes.Ldloc, locTypedObj);
- il.Emit(OpCodes.Castclass, typeof(IJsonWriting));
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriting).GetMethod("OnJsonWriting", new Type[] { typeof(IJsonWriter) }));
- }
- }
- // Process all members
- foreach (var m in ri.Members)
- {
- // Dont save deprecated properties
- if (m.Deprecated)
- {
- continue;
- }
- // Ignore write only properties
- var pi = m.Member as PropertyInfo;
- if (pi != null && pi.GetGetMethod(true) == null)
- {
- continue;
- }
- // Write the Json key
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Ldstr, m.JsonKey);
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteKeyNoEscaping", new Type[] { typeof(string) }));
- // Load the writer
- il.Emit(OpCodes.Ldarg_0);
- // Get the member type
- var memberType = m.MemberType;
- // Load the target object
- if (type.IsValueType)
- {
- il.Emit(OpCodes.Ldloca, locTypedObj);
- }
- else
- {
- il.Emit(OpCodes.Ldloc, locTypedObj);
- }
- // Work out if we need the value or it's address on the stack
- bool NeedValueAddress = (memberType.IsValueType && (toStringTypes.Contains(memberType) || otherSupportedTypes.Contains(memberType)));
- if (Nullable.GetUnderlyingType(memberType) != null)
- {
- NeedValueAddress = true;
- }
- // Property?
- if (pi != null)
- {
- // Call property's get method
- if (type.IsValueType)
- il.Emit(OpCodes.Call, pi.GetGetMethod(true));
- else
- il.Emit(OpCodes.Callvirt, pi.GetGetMethod(true));
- // If we need the address then store in a local and take it's address
- if (NeedValueAddress)
- {
- var locTemp = il.DeclareLocal(memberType);
- il.Emit(OpCodes.Stloc, locTemp);
- il.Emit(OpCodes.Ldloca, locTemp);
- }
- }
- // Field?
- var fi = m.Member as FieldInfo;
- if (fi != null)
- {
- if (NeedValueAddress)
- {
- il.Emit(OpCodes.Ldflda, fi);
- }
- else
- {
- il.Emit(OpCodes.Ldfld, fi);
- }
- }
- Label? lblFinished = null;
- // Is it a nullable type?
- var typeUnderlying = Nullable.GetUnderlyingType(memberType);
- if (typeUnderlying != null)
- {
- // Duplicate the address so we can call get_HasValue() and then get_Value()
- il.Emit(OpCodes.Dup);
- // Define some labels
- var lblHasValue = il.DefineLabel();
- lblFinished = il.DefineLabel();
- // Call has_Value
- il.Emit(OpCodes.Call, memberType.GetProperty("HasValue").GetGetMethod());
- il.Emit(OpCodes.Brtrue, lblHasValue);
- // No value, write "null:
- il.Emit(OpCodes.Pop);
- il.Emit(OpCodes.Ldstr, "null");
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
- il.Emit(OpCodes.Br_S, lblFinished.Value);
- // Get it's value
- il.MarkLabel(lblHasValue);
- il.Emit(OpCodes.Call, memberType.GetProperty("Value").GetGetMethod());
- // Switch to the underlying type from here on
- memberType = typeUnderlying;
- NeedValueAddress = (memberType.IsValueType && (toStringTypes.Contains(memberType) || otherSupportedTypes.Contains(memberType)));
- // Work out again if we need the address of the value
- if (NeedValueAddress)
- {
- var locTemp = il.DeclareLocal(memberType);
- il.Emit(OpCodes.Stloc, locTemp);
- il.Emit(OpCodes.Ldloca, locTemp);
- }
- }
- // ToString()
- if (toStringTypes.Contains(memberType))
- {
- // Convert to string
- il.Emit(OpCodes.Ldloc, locInvariant);
- il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { typeof(IFormatProvider) }));
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
- }
- // ToString("R")
- else if (memberType == typeof(float) || memberType == typeof(double))
- {
- il.Emit(OpCodes.Ldstr, "R");
- il.Emit(OpCodes.Ldloc, locInvariant);
- il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { typeof(string), typeof(IFormatProvider) }));
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
- }
- // String?
- else if (memberType == typeof(string))
- {
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral", new Type[] { typeof(string) }));
- }
- // Char?
- else if (memberType == typeof(char))
- {
- il.Emit(OpCodes.Call, memberType.GetMethod("ToString", new Type[] { }));
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteStringLiteral", new Type[] { typeof(string) }));
- }
- // Bool?
- else if (memberType == typeof(bool))
- {
- var lblTrue = il.DefineLabel();
- var lblCont = il.DefineLabel();
- il.Emit(OpCodes.Brtrue_S, lblTrue);
- il.Emit(OpCodes.Ldstr, "false");
- il.Emit(OpCodes.Br_S, lblCont);
- il.MarkLabel(lblTrue);
- il.Emit(OpCodes.Ldstr, "true");
- il.MarkLabel(lblCont);
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteRaw", new Type[] { typeof(string) }));
- }
- // NB: We don't support DateTime as it's format can be changed
- else
- {
- // Unsupported type, pass through
- if (memberType.IsValueType)
- {
- il.Emit(OpCodes.Box, memberType);
- }
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriter).GetMethod("WriteValue", new Type[] { typeof(object) }));
- }
- if (lblFinished.HasValue)
- il.MarkLabel(lblFinished.Value);
- }
- // Call IJsonWritten
- if (typeof(IJsonWritten).IsAssignableFrom(type))
- {
- if (type.IsValueType)
- {
- il.Emit(OpCodes.Ldloca, locTypedObj);
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, type.GetInterfaceMap(typeof(IJsonWritten)).TargetMethods[0]);
- }
- else
- {
- il.Emit(OpCodes.Ldloc, locTypedObj);
- il.Emit(OpCodes.Castclass, typeof(IJsonWriting));
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Callvirt, typeof(IJsonWriting).GetMethod("OnJsonWritten", new Type[] { typeof(IJsonWriter) }));
- }
- }
- // Done!
- il.Emit(OpCodes.Ret);
- var impl = (Action<IJsonWriter, object>)method.CreateDelegate(typeof(Action<IJsonWriter, object>));
- // Wrap it in a call to WriteDictionary
- return (w, obj) =>
- {
- w.WriteDictionary(() =>
- {
- impl(w, obj);
- });
- };
- }
- }
- // Pseudo box lets us pass a value type by reference. Used during
- // deserialization of value types.
- interface IPseudoBox
- {
- object GetValue();
- }
- [Obfuscation(Exclude = true, ApplyToMembers = true)]
- class PseudoBox<T> : IPseudoBox where T : struct
- {
- public T value = default(T);
- object IPseudoBox.GetValue() { return value; }
- }
- // Make a parser for value types
- public static Func<IJsonReader, Type, object> MakeParser(Type type)
- {
- System.Diagnostics.Debug.Assert(type.IsValueType);
- // ParseJson method?
- var parseJson = ReflectionInfo.FindParseJson(type);
- if (parseJson != null)
- {
- if (parseJson.GetParameters()[0].ParameterType == typeof(IJsonReader))
- {
- var method = new DynamicMethod("invoke_ParseJson", typeof(Object), new Type[] { typeof(IJsonReader), typeof(Type) }, true);
- var il = method.GetILGenerator();
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, parseJson);
- il.Emit(OpCodes.Box, type);
- il.Emit(OpCodes.Ret);
- return (Func<IJsonReader,Type,object>)method.CreateDelegate(typeof(Func<IJsonReader,Type,object>));
- }
- else
- {
- var method = new DynamicMethod("invoke_ParseJson", typeof(Object), new Type[] { typeof(string) }, true);
- var il = method.GetILGenerator();
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, parseJson);
- il.Emit(OpCodes.Box, type);
- il.Emit(OpCodes.Ret);
- var invoke = (Func<string, object>)method.CreateDelegate(typeof(Func<string, object>));
- return (r, t) =>
- {
- if (r.GetLiteralKind() == LiteralKind.String)
- {
- var o = invoke(r.GetLiteralString());
- r.NextToken();
- return o;
- }
- throw new InvalidDataException(string.Format("Expected string literal for type {0}", type.FullName));
- };
- }
- }
- else
- {
- // Get the reflection info for this type
- var ri = ReflectionInfo.GetReflectionInfo(type);
- if (ri == null)
- return null;
- // We'll create setters for each property/field
- var setters = new Dictionary<string, Action<IJsonReader, object>>();
- // Store the value in a pseudo box until it's fully initialized
- var boxType = typeof(PseudoBox<>).MakeGenericType(type);
- // Process all members
- foreach (var m in ri.Members)
- {
- // Ignore write only properties
- var pi = m.Member as PropertyInfo;
- var fi = m.Member as FieldInfo;
- if (pi != null && pi.GetSetMethod(true) == null)
- {
- continue;
- }
- // Create a dynamic method that can do the work
- var method = new DynamicMethod("dynamic_parser", null, new Type[] { typeof(IJsonReader), typeof(object) }, true);
- var il = method.GetILGenerator();
- // Load the target
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Castclass, boxType);
- il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
- // Get the value
- GenerateGetJsonValue(m, il);
- // Assign it
- if (pi != null)
- il.Emit(OpCodes.Call, pi.GetSetMethod(true));
- if (fi != null)
- il.Emit(OpCodes.Stfld, fi);
- // Done
- il.Emit(OpCodes.Ret);
- // Store in the map of setters
- setters.Add(m.JsonKey, (Action<IJsonReader, object>)method.CreateDelegate(typeof(Action<IJsonReader, object>)));
- }
- // Create helpers to invoke the interfaces (this is painful but avoids having to really box
- // the value in order to call the interface).
- Action<object, IJsonReader> invokeLoading = MakeInterfaceCall(type, typeof(IJsonLoading));
- Action<object, IJsonReader> invokeLoaded = MakeInterfaceCall(type, typeof(IJsonLoaded));
- Func<object, IJsonReader, string, bool> invokeField = MakeLoadFieldCall(type);
- // Create the parser
- Func<IJsonReader, Type, object> parser = (reader, Type) =>
- {
- // Create pseudobox (ie: new PseudoBox<Type>)
- var box = DecoratingActivator.CreateInstance(boxType);
- // Call IJsonLoading
- if (invokeLoading != null)
- invokeLoading(box, reader);
- // Read the dictionary
- reader.ParseDictionary(key =>
- {
- // Call IJsonLoadField
- if (invokeField != null && invokeField(box, reader, key))
- return;
- // Get a setter and invoke it if found
- Action<IJsonReader, object> setter;
- if (setters.TryGetValue(key, out setter))
- {
- setter(reader, box);
- }
- });
- // IJsonLoaded
- if (invokeLoaded != null)
- invokeLoaded(box, reader);
- // Return the value
- return ((IPseudoBox)box).GetValue();
- };
- // Done
- return parser;
- }
- }
- // Helper to make the call to a PsuedoBox value's IJsonLoading or IJsonLoaded
- static Action<object, IJsonReader> MakeInterfaceCall(Type type, Type tItf)
- {
- // Interface supported?
- if (!tItf.IsAssignableFrom(type))
- return null;
- // Resolve the box type
- var boxType = typeof(PseudoBox<>).MakeGenericType(type);
- // Create method
- var method = new DynamicMethod("dynamic_invoke_" + tItf.Name, null, new Type[] { typeof(object), typeof(IJsonReader) }, true);
- var il = method.GetILGenerator();
- // Call interface method
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Castclass, boxType);
- il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Call, type.GetInterfaceMap(tItf).TargetMethods[0]);
- il.Emit(OpCodes.Ret);
- // Done
- return (Action<object, IJsonReader>)method.CreateDelegate(typeof(Action<object, IJsonReader>));
- }
- // Similar to above but for IJsonLoadField
- static Func<object, IJsonReader, string, bool> MakeLoadFieldCall(Type type)
- {
- // Interface supported?
- var tItf = typeof(IJsonLoadField);
- if (!tItf.IsAssignableFrom(type))
- return null;
- // Resolve the box type
- var boxType = typeof(PseudoBox<>).MakeGenericType(type);
- // Create method
- var method = new DynamicMethod("dynamic_invoke_" + tItf.Name, typeof(bool), new Type[] { typeof(object), typeof(IJsonReader), typeof(string) }, true);
- var il = method.GetILGenerator();
- // Call interface method
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Castclass, boxType);
- il.Emit(OpCodes.Ldflda, boxType.GetField("value"));
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Ldarg_2);
- il.Emit(OpCodes.Call, type.GetInterfaceMap(tItf).TargetMethods[0]);
- il.Emit(OpCodes.Ret);
- // Done
- return (Func<object, IJsonReader, string, bool>)method.CreateDelegate(typeof(Func<object, IJsonReader, string, bool>));
- }
- // Create an "into parser" that can parse from IJsonReader into a reference type (ie: a class)
- public static Action<IJsonReader, object> MakeIntoParser(Type type)
- {
- System.Diagnostics.Debug.Assert(!type.IsValueType);
- // Get the reflection info for this type
- var ri = ReflectionInfo.GetReflectionInfo(type);
- if (ri == null)
- return null;
- // We'll create setters for each property/field
- var setters = new Dictionary<string, Action<IJsonReader, object>>();
- // Process all members
- foreach (var m in ri.Members)
- {
- // Ignore write only properties
- var pi = m.Member as PropertyInfo;
- var fi = m.Member as FieldInfo;
- if (pi != null && pi.GetSetMethod(true) == null)
- {
- continue;
- }
- // Ignore read only properties that has KeepInstance attribute
- if (pi != null && pi.GetGetMethod(true) == null && m.KeepInstance)
- {
- continue;
- }
- // Create a dynamic method that can do the work
- var method = new DynamicMethod("dynamic_parser", null, new Type[] { typeof(IJsonReader), typeof(object) }, true);
- var il = method.GetILGenerator();
- // Load the target
- il.Emit(OpCodes.Ldarg_1);
- il.Emit(OpCodes.Castclass, type);
- // Try to keep existing instance?
- if (m.KeepInstance)
- {
- // Get existing existing instance
- il.Emit(OpCodes.Dup);
- if (pi != null)
- il.Emit(OpCodes.Callvirt, pi.GetGetMethod(true));
- else
- il.Emit(OpCodes.Ldfld, fi);
- var existingInstance = il.DeclareLocal(m.MemberType);
- var lblExistingInstanceNull = il.DefineLabel();
- // Keep a copy of the existing instance in a locale
- il.Emit(OpCodes.Dup);
- il.Emit(OpCodes.Stloc, existingInstance);
- // Compare to null
- il.Emit(OpCodes.Ldnull);
- il.Emit(OpCodes.Ceq);
- il.Emit(OpCodes.Brtrue_S, lblExistingInstanceNull);
- il.Emit(OpCodes.Ldarg_0); // reader
- il.Emit(OpCodes.Ldloc, existingInstance); // into
- il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("ParseInto", new Type[] { typeof(Object) }));
- il.Emit(OpCodes.Pop); // Clean up target left on stack (1)
- il.Emit(OpCodes.Ret);
- il.MarkLabel(lblExistingInstanceNull);
- }
- // Get the value from IJsonReader
- GenerateGetJsonValue(m, il);
- // Assign it
- if (pi != null)
- il.Emit(OpCodes.Callvirt, pi.GetSetMethod(true));
- if (fi != null)
- il.Emit(OpCodes.Stfld, fi);
- // Done
- il.Emit(OpCodes.Ret);
- // Store the handler in map
- setters.Add(m.JsonKey, (Action<IJsonReader, object>)method.CreateDelegate(typeof(Action<IJsonReader, object>)));
- }
- // Now create the parseInto delegate
- Action<IJsonReader, object> parseInto = (reader, obj) =>
- {
- // Call IJsonLoading
- var loading = obj as IJsonLoading;
- if (loading != null)
- loading.OnJsonLoading(reader);
- // Cache IJsonLoadField
- var lf = obj as IJsonLoadField;
- // Read dictionary keys
- reader.ParseDictionary(key =>
- {
- // Call IJsonLoadField
- if (lf != null && lf.OnJsonField(reader, key))
- return;
- // Call setters
- Action<IJsonReader, object> setter;
- if (setters.TryGetValue(key, out setter))
- {
- setter(reader, obj);
- }
- });
- // Call IJsonLoaded
- var loaded = obj as IJsonLoaded;
- if (loaded != null)
- loaded.OnJsonLoaded(reader);
- };
- // Since we've created the ParseInto handler, we might as well register
- // as a Parse handler too.
- RegisterIntoParser(type, parseInto);
- // Done
- return parseInto;
- }
- // Registers a ParseInto handler as Parse handler that instantiates the object
- // and then parses into it.
- static void RegisterIntoParser(Type type, Action<IJsonReader, object> parseInto)
- {
- // Check type has a parameterless constructor
- var con = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null);
- if (con == null)
- return;
- // Create a dynamic method that can do the work
- var method = new DynamicMethod("dynamic_factory", typeof(object), new Type[] { typeof(IJsonReader), typeof(Action<IJsonReader, object>) }, true);
- var il = method.GetILGenerator();
- // Create the new object
- var locObj = il.DeclareLocal(typeof(object));
- il.Emit(OpCodes.Newobj, con);
- il.Emit(OpCodes.Dup); // For return value
- il.Emit(OpCodes.Stloc, locObj);
- il.Emit(OpCodes.Ldarg_1); // parseinto delegate
- il.Emit(OpCodes.Ldarg_0); // IJsonReader
- il.Emit(OpCodes.Ldloc, locObj); // new object instance
- il.Emit(OpCodes.Callvirt, typeof(Action<IJsonReader, object>).GetMethod("Invoke"));
- il.Emit(OpCodes.Ret);
- var factory = (Func<IJsonReader, Action<IJsonReader, object>, object>)method.CreateDelegate(typeof(Func<IJsonReader, Action<IJsonReader, object>, object>));
- Json.RegisterParser(type, (reader, type2) =>
- {
- return factory(reader, parseInto);
- });
- }
- // Generate the MSIL to retrieve a value for a particular field or property from a IJsonReader
- private static void GenerateGetJsonValue(JsonMemberInfo m, ILGenerator il)
- {
- Action<string> generateCallToHelper = helperName =>
- {
- // Call the helper
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, typeof(Emit).GetMethod(helperName, new Type[] { typeof(IJsonReader) }));
- // Move to next token
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
- };
- Type[] numericTypes = new Type[] {
- typeof(int), typeof(uint), typeof(long), typeof(ulong),
- typeof(short), typeof(ushort), typeof(decimal),
- typeof(byte), typeof(sbyte),
- typeof(double), typeof(float)
- };
- if (m.MemberType == typeof(string))
- {
- generateCallToHelper("GetLiteralString");
- }
- else if (m.MemberType == typeof(bool))
- {
- generateCallToHelper("GetLiteralBool");
- }
- else if (m.MemberType == typeof(char))
- {
- generateCallToHelper("GetLiteralChar");
- }
- else if (numericTypes.Contains(m.MemberType))
- {
- // Get raw number string
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Call, typeof(Emit).GetMethod("GetLiteralNumber", new Type[] { typeof(IJsonReader) }));
- // Convert to a string
- il.Emit(OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod());
- il.Emit(OpCodes.Call, m.MemberType.GetMethod("Parse", new Type[] { typeof(string), typeof(IFormatProvider) }));
- //
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
- }
- else
- {
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Ldtoken, m.MemberType);
- il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }));
- il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("Parse", new Type[] { typeof(Type) }));
- il.Emit(m.MemberType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, m.MemberType);
- }
- }
- // Helper to fetch a literal bool from an IJsonReader
- [Obfuscation(Exclude = true)]
- public static bool GetLiteralBool(IJsonReader r)
- {
- switch (r.GetLiteralKind())
- {
- case LiteralKind.True:
- return true;
- case LiteralKind.False:
- return false;
- default:
- throw new InvalidDataException("expected a boolean value");
- }
- }
- // Helper to fetch a literal character from an IJsonReader
- [Obfuscation(Exclude = true)]
- public static char GetLiteralChar(IJsonReader r)
- {
- if (r.GetLiteralKind() != LiteralKind.String)
- throw new InvalidDataException("expected a single character string literal");
- var str = r.GetLiteralString();
- if (str == null || str.Length != 1)
- throw new InvalidDataException("expected a single character string literal");
- return str[0];
- }
- // Helper to fetch a literal string from an IJsonReader
- [Obfuscation(Exclude = true)]
- public static string GetLiteralString(IJsonReader r)
- {
- switch (r.GetLiteralKind())
- {
- case LiteralKind.Null: return null;
- case LiteralKind.String: return r.GetLiteralString();
- }
- throw new InvalidDataException("expected a string literal");
- }
- // Helper to fetch a literal number from an IJsonReader (returns the raw string)
- [Obfuscation(Exclude = true)]
- public static string GetLiteralNumber(IJsonReader r)
- {
- switch (r.GetLiteralKind())
- {
- case LiteralKind.SignedInteger:
- case LiteralKind.UnsignedInteger:
- case LiteralKind.FloatingPoint:
- return r.GetLiteralString();
- }
- throw new InvalidDataException("expected a numeric literal");
- }
- }
- #endif
- }
- }
|