Register Json data in FastReport.NET

2015-03-23

If you need to register Json as data source in FastReport.NET, this can be done through the registration of business objects.
For parsing json you need to describe the data scheme in the C# classes, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 public class Product
 {
 public string Name { get; set; }
 public double UnitPrice { get; set; }
 }
 
 public class Category
 {
 public string Name { get; set; }
 public string Description { get; set; }
 public List<Product> Products { get; set; }
 }
 
 string json = "..."; // your json
 
 List<Category> categories = JsonConvert.DeserializeObject<List<Category>>(json); // using the Newtonsoft.Json library for deserialization
 
 Report report = new Report();
 report.Load(@"C:\report.frx");
 report.RegisterData(categories, "Categories");

If you do not know the scheme or if it is changing during the work of the program, it can be generated dynamically. For this we use the library JSON C# Class Generator, you can download it from https://jsonclassgenerator.codeplex.com.
Code to generate C# classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 string json = "..."; // your json
 
 if (json.StartsWith("[")) // if json is an array, then it would be better if we name it
 {
 json = "{\"Data\":" + json + "}"; // Under this name the data will be displayed in designer, so you can add multiple data sources
 }
 
 JsonClassGenerator gen = new JsonClassGenerator()
 {
 Example = json,
 UseProperties = true,
 Namespace = "__JSON__",
 MainClass = "__JSON__",
 };
 
 string source = "";
 using (StringWriter sw = new StringWriter())
 {
 gen.OutputStream = sw;
 gen.GenerateClasses();
 sw.Flush();
 source = sw.ToString();
 }
As a result we obtain the generated classes in a string. Now we need to access them from the code; in order to do this compile them using CSharpCodeProvider and get Type:
1
2
3
4
5
6
7
8
9
10
 Type type = null;
 using (CSharpCodeProvider compiler = new CSharpCodeProvider())
 {
 CompilerParameters parameters = new CompilerParameters()
 {
 GenerateInMemory = true,
 };
 CompilerResults results = compiler.CompileAssemblyFromSource(parameters, source);
 type = results.CompiledAssembly.GetType("__JSON__.__JSON__");
 }

Note: This example shows a compilation in the current AppDomain. This means that it can not be unloaded from memory, and each compilation will eat off memory. To avoid this, you can use CSharpCodeProvider in another AppDomain.

Deserialize json using the generated classes. The easiest way to do this is to use the library Newtonsoft.Json; You can get it through NuGet with the command "Install-Package Newtonsoft.Json".

1
 object obj = JsonConvert.DeserializeObject(json, type);
Now we have objects, so let's register them in FastReport.NET:
1
2
3
4
5
6
7
8
9
 PropertyInfo[] properties = type.GetProperties();
 
 Report report = new Report();
 report.Load(@"C:\report.frx");
 
 foreach (var prop in properties)
 {
 report.RegisterData((IList)prop.GetValue(obj, null), prop.Name);
 }

So now you can run designer and see the results.

Data

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.