Json regex deserialization with JSON.Net -


i'm trying read following example json text file string using json.net parsing library.

content of c:\temp\regelib.json

{     "regular expressions library":     {       "sampleregex":"^(?<field1>\d+)_(?<field2>\d+)_(?<field3>[\w\&-]+)_(?<field4>\w+).txt$"     } } 

example code try , parse:

newtonsoft.json.converters.regexconverter rconv = new newtonsoft.json.converters.regexconverter();         using (streamreader reader = file.opentext(libpath))         {           string foo = reader.readtoend();           jobject jo = jobject.parse(foo);//<--error            //how use regexconverter parse??           newtonsoft.json.jsontextreader jtr = new newtonsoft.json.jsontextreader(reader);           jobject test = rconv.readjson(jtr);//<--not sure parameters provide           string sampleregex = test.tostring();         } 

it seems need use converter, know code above wrong, can't find examples describe how / if can done. possible read regular expression token text file string using json.net? appreciated.

update:

played more , figured out had escape character classes, once made correction below able parse jobject , use linq query regex pattern.

corrected content c:\temp\regelib.json

{     "regular expressions library":     {       "sampleregex":"^(?<field1>\\d+)_(?<field2>\\d+)_(?<field3>[\\w\\&-]+)_(?<field4>\\w+).txt$"     } } 

corrected code

using (streamreader reader = file.opentext(libpath))         {           string content = reader.readtoend().trim();           jobject regexlib = jobject.parse(content);           string sampleregex = regexlib["regular expressions library"]["sampleregex"].tostring();            //which lets me following...           regex rsampleregex = new regex(sampleregex);            foreach (string samplefilepath in directory.getfiles(dirsamplefiles, "*"))           {             filename = path.getfilename(samplefilepath);             if (rsampleregex.ismatch(filename))             {               //do stuff...             }           }         } 

not sure if best approach, seems work case.

i don't understand why have store such small regex in json file, going expand regex in future?

if so, rather doing this

jobject regexlib = jobject.parse(content); string sampleregex = regexlib["regular expressions library"]["sampleregex"].tostring(); 

consider using json2csharp make classes, @ least it's strongly-typed , make more maintainable.

i think more appropriate json (assumptions):

{     "regular expressions library": [         {           "sampleregex": "^(?<field1>\\d+)_(?<field2>\\d+)_(?<field3>[\\w\\&-]+)_(?<field4>\\w+).txt$"         },         {           "sampleregex2": "^(?<field1>\\d+)_(?<field2>\\d+)_(?<field3>[\\w\\&-]+)_(?<field4>\\w+).txt$"         }     ] } 

it make more sense way store regex in "settings" file


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -