Code maitained in github https://github.com/WonderTools/JsonSectionReader
Product released in NuGet https://www.nuget.org/packages/WonderTools.JsonSectionReader/
Sample usage https://github.com/WonderTools/JsonSectionReaderUsage
While unit testing, the amount of test data could become voluminious and could easily make the test code less readable. Having the data separated from code imporves the readability of the tests in my opinion. Yes... This is an opinionated tool, and the opinions that shape this tool are listed below.
While unit testing, the test data could be huge and when coded with the test cases, it makes the test case less readable. Having the test data separated makes the test data and test code more readable. The tool facilitates the storing the test data in a separate file.
The test data has lot of information about the tests, and it's really important for this to be readable and easy to modify. The tool facilitates this by having the test data is a json file.
The test data must be easily accessible from the test case. Having the test data packaged in another entity would make such as a file or data base would increase the probability of errors such as "File not found", "Invalid Connection String", etc... To make the unit testing more reliable the test data and test code has to be packaged together, and test data must be quickly accessible from test code. The tool facilites this by letting your test data be stored in the same library as embedded resource.
- Reads full or sections of JSON file
- Reads JSON file stored as embedded resource.
- Make deserializion of JSON to .Net objects easy
- Capable of reading various encoding formats such as UTF8, UTF32, etc...
- Supports for data in other languages (Non-ASCII characters)
- Supports tabular representation of data (similar to data from database)
- Add reference to the nuget package "WonderTools.JsonSectionReader"
- Add a json file to your project say "TestData.json"
- Make this json file as an embedded resource.
- Add the json data that you would like it to be available
- Call the JsonSectionReader methods to get a section of the data and to deserialize it. (Explained with examples below)
- The examples shown below are available in the github repository https://github.com/WonderTools/JsonSectionReaderUsage
Example1Sectioning.json
{
"name": "john",
"age" : 32
}
JSectionReader.Section("Example1Sectioning.json").GetSection("name").GetObject<string>(); JSectionReader.Section("Example1Sectioning.json", Encoding.Default, "name").GetObject<string>(); new JSectionReader().Read("Example1Sectioning.json", Encoding.Default, "name").GetObject<string>();All of the above lines return string "john".
Example2Sectioning.json
{
"employees": [
{
"name": "philip",
"age": 28
},
{
"name": "richard",
"age": 31
}
]
} JSectionReader.Section("Example2Sectioning.json").GetSection("employees", 1, "name").GetObject<string>();The statement return string "richard"
Example3Encoding.json
{
"words" : [ "good", "bad", "Mädchen" ]
} JSectionReader.Section("Example3Encoding.json").GetSection("words", 2).GetObject<string>();- The encoding of the file is set to UTF-8 (using Notepad++)
- The statement return the string "Mädchen"
JsonSectionReaderUsage.Example4FileDiscovery.Foo.Example4FileDiscovery.json
{
"animal": "lion"
}JsonSectionReaderUsage.Example4FileDiscovery.Boo.Example4FileDiscovery.json
{
"animal" : "elephant"
} JSectionReader
.Section("JsonSectionReaderUsage.Example4FileDiscovery.Boo.Example4FileDiscovery.json")
.GetSection("animal").GetObject<string>(); JSectionReader
.Section("Example4FileDiscovery.Boo.Example4FileDiscovery.json")
.GetSection("animal").GetObject<string>(); JSectionReader
.Section("Boo.Example4FileDiscovery.json")
.GetSection("animal").GetObject<string>();- There are two embedded resources named Example4FileDiscovery.json, so specifing file name as "Example4FileDiscovery.json" will be ambigious and would result in an exception.
- The file named has to be more specifically mentioned to avoid ambigiouty.
- The valid names for identifying the file are
- JsonSectionReaderUsage.Example4FileDiscovery.Boo.Example4FileDiscovery.json
- Example4FileDiscovery.Boo.Example4FileDiscovery.json
- Boo.Example4FileDiscovery.json
- All of the above c sharp statements return the string "elephant"
Example5ReadingObject.json
{
"person" : {
"name": "richard",
"age" : 22
}
}JSectionReader.Section("Example5ReadingObject.json").GetSection("person").GetObject<Person>();- The statement returns a Person object with name as "richard" and Age as 22.
Example6ReadingList.json
{
"numbers" : [5,4,3,2,1]
}JSectionReader.Section("Example6ReadingList.json").GetSection("numbers").GetObject<List<int>>();- The statment returns a List with 5, 4, 3, 2, 1 in it.
Example7ReadingAsJson.json
{
"employees": [
{
"name": "John",
"id": 31432
},
{
"name": "Nash",
"id": 31433
}
]
}new JSectionReader().Read("Example7ReadingAsJson.json").GetSection("employees" , 1).GetJson();- The statement returns a string {"name":"Nash","id":31433}
Reading a table as list of list of objects
Example8ListOfList.json
{
"data": [
[ 1, "Monday", "Morning" ],
[ 2, "Monday", "Afternoon" ],
[ 3, "Tuesday", "Morning" ],
[ 4, "Tuesday", "Afternoon" ]
]
} new JSectionReader().Read("Example8ListOfList.json").GetSection("data")
.GetTable(typeof(int), typeof(string), typeof(string)) new List<List<object>>()
{
new List<object>() {1, "Monday", "Morning"},
new List<object>() {2, "Monday", "Afternoon"},
new List<object>() {3, "Tuesday", "Morning"},
new List<object>() {4, "Tuesday", "Afternoon"},
};The statement returns a List<List> as shown above
Reading a table as list of user defined objects
Example9ListOfObject.json
{
"data": [
[ 2432, "John", 32 ],
[ 2222, "Nash", 33 ],
[ 3421, "Peter", 33 ]
]
}
new JSectionReader().Read("Example9ListOfObject.json").GetSection("data")
.GetTableAsObjectList<Employee, int, string, int>(
(id, name, age) => new Employee()
{
Id = id,
Name = name,
Age = age,
}); new List<Employee>()
{
new Employee(){Id =2432,Name ="John", Age = 32},
new Employee(){Id =2222,Name ="Nash", Age = 33},
new Employee(){Id =3421,Name ="Peter", Age = 33},
};The statement returns a list of employees as shown above