You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.5 KiB
143 lines
4.5 KiB
---
|
|
sidebar_position: 3
|
|
title: Fake data
|
|
---
|
|
|
|
## Generation of fake data
|
|
|
|
In order to be able to start using your site, we will generate fake data to simulate a connection to an API.
|
|
|
|
To do this, create a new `fake-data.json` file at the root of the `wwwroot` directory:
|
|
|
|

|
|
|
|
## Data representation class
|
|
|
|
Your data must match the output format of your future API.
|
|
|
|
Create a `Models` folder of your project then create the `Item.cs` class:
|
|
|
|

|
|
|
|
This class will represent the object version of your data.
|
|
|
|
Class content:
|
|
|
|
```csharp title="Models/Item.cs"
|
|
public class Item
|
|
{
|
|
public int Id { get; set; }
|
|
public string DisplayName { get; set; }
|
|
public string Name { get; set; }
|
|
public int StackSize { get; set; }
|
|
public int MaxDurability { get; set; }
|
|
public List<string> EnchantCategories { get; set; }
|
|
public List<string> RepairWith { get; set; }
|
|
public DateTime CreatedDate { get; set; }
|
|
public DateTime? UpdatedDate { get; set; }
|
|
}
|
|
```
|
|
|
|
## How to Generate Fake Data Easily
|
|
|
|
In order to generate random data we can use the following site [https://json-generator.com/](https://json-generator.com/).
|
|
|
|
The template to use for the class is:
|
|
|
|
```json
|
|
[
|
|
'{{repeat(15, 30)}}',
|
|
{
|
|
id: '{{index(1)}}',
|
|
displayname: '{{company()}}',
|
|
name: function () {
|
|
return this.displayname.toLowerCase();
|
|
},
|
|
stacksize: '{{integer(1, 64)}}',
|
|
maxdurability: '{{integer(1, 125)}}',
|
|
enchantcategories: [
|
|
'{{repeat(0, 3)}}',
|
|
'{{random("armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable")}}'
|
|
],
|
|
repairwith: [
|
|
'{{repeat(0, 2)}}',
|
|
'{{random("oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks")}}'
|
|
],
|
|
createddate: '{{date(new Date(2014, 0, 1), new Date(), "YYYY-MM-dd")}}',
|
|
updateddate: '{{random(date(new Date(2014, 0, 1), new Date(), "YYYY-MM-dd"), null)}}'
|
|
}
|
|
]
|
|
```
|
|
|
|
Copy the result of the generation into the `fake-data.json` file.
|
|
|
|
:::caution
|
|
Your data must start with `[` and end with `]`.
|
|
Example:
|
|
|
|
```json
|
|
[
|
|
{
|
|
"id": 1,
|
|
"displayname": "Furnafix",
|
|
"name": "furnafix",
|
|
"stacksize": 36,
|
|
"maxdurability": 32,
|
|
"enchantcategories": [],
|
|
"repairwith": [],
|
|
"createddate": "2019-02-16",
|
|
"updateddate": null
|
|
},
|
|
{
|
|
"id": 2,
|
|
"displayname": "Deminimum",
|
|
"name": "deminimum",
|
|
"stacksize": 49,
|
|
"maxdurability": 46,
|
|
"enchantcategories": [],
|
|
"repairwith": [],
|
|
"createddate": "2020-06-14",
|
|
"updateddate": null
|
|
},
|
|
...
|
|
]
|
|
```
|
|
|
|
:::
|
|
|
|
## Concept: Serialization
|
|
|
|
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.
|
|
Its main purpose is to save the state of an object so that it can be recreated if necessary. The reverse process is called deserialization.
|
|
|
|
### How serialization works
|
|
|
|
This illustration shows the overall serialization process:
|
|
|
|

|
|
|
|
The object is serialized into a stream that contains the data. The stream can also contain information about the type of the object, such as its version, culture, and assembly name.
|
|
From this stream, the object can be stored in a database, file or memory.
|
|
|
|
### Uses of serialization
|
|
|
|
Serialization allows the developer to save the state of an object and recreate it as needed, providing object storage as well as data exchange.
|
|
|
|
Through serialization, a developer can perform actions such as:
|
|
* Sending the object to a remote application using a web service
|
|
* Passing an object from one domain to another
|
|
* Passing an object through a firewall as a JSON or XML string
|
|
* Manage security or user information between applications
|
|
|
|
### JSON serialization with C#
|
|
|
|
The `System.Text.Json` namespace contains classes for JavaScript Object Notation (JSON) serialization and deserialization.
|
|
JSON is an open standard that is commonly used to share data on the web.
|
|
|
|
JSON serialization serializes an object's public properties into a string, byte array, or stream that conforms to the [JSON RFC 8259](https://tools.ietf.org/html/rfc8259) specification.
|
|
|
|
To control how JsonSerializer serializes or deserializes an instance of the class:
|
|
* Use a `JsonSerializerOptions` object
|
|
* Apply attributes from the `System.Text.Json.Serialization` namespace to classes or properties
|
|
* Implement custom converters
|