Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,17 @@
"contributions": [
"code"
]
},
{
"login": "fr-th",
"name": "fr-th",
"avatar_url": "https://avatars.githubusercontent.com/u/61010557?v=4",
"profile": "https://github.com/fr-th",
"contributions": [
"code",
"test",
"doc"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions)
[![Website](https://img.shields.io/website?label=website&url=https%3A%2F%2Fwww.modelina.org)](https://www.modelina.org)
[![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.modelina.org%2Fplayground)](https://www.modelina.org/playground) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-99-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-100-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->


Expand Down Expand Up @@ -452,6 +452,7 @@ Thanks go out to these wonderful people ([emoji key](https://allcontributors.org
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Shriya-Chauhan"><img src="https://avatars.githubusercontent.com/u/78415084?v=4?s=100" width="100px;" alt="Shriya Chauhan"/><br /><sub><b>Shriya Chauhan</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=Shriya-Chauhan" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/fr-th"><img src="https://avatars.githubusercontent.com/u/61010557?v=4?s=100" width="100px;" alt="fr-th"/><br /><sub><b>fr-th</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=fr-th" title="Code">💻</a> <a href="https://github.com/asyncapi/modelina/commits?author=fr-th" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/modelina/commits?author=fr-th" title="Documentation">📖</a></td>
</tr>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions docs/languages/Csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Requires [System.Text.Json](https://devblogs.microsoft.com/dotnet/try-the-new-sy
#### Using Newtonsoft/Json.NET

To include functionality that convert the models using the [Newtonsoft/Json.NET](https://www.newtonsoft.com/json) framework, to use this, use the preset `CSHARP_NEWTONSOFT_SERIALIZER_PRESET`.
You can use the option "enforceRequired" to prevent deserialization if any required field is missing.

Check out this [example for a live demonstration](../../examples/csharp-generate-newtonsoft-serializer).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ Array [
"[JsonConverter(typeof(RootConverter))]
public partial class Root
{
private string? email;
private string email;
private string? name;

public string? Email
public string Email
{
get { return email; }
set { this.email = value; }
}

public string? Name
{
get { return name; }
set { this.name = value; }
}

public string Serialize()
{
return JsonConvert.SerializeObject(this);
Expand All @@ -30,8 +37,14 @@ public class RootConverter : JsonConverter<Root>
JObject jo = JObject.Load(reader);
Root value = new Root();

if(jo[\\"email\\"] != null) {
value.Email = jo[\\"email\\"].ToObject<string?>(serializer);
if(jo[\\"email\\"] is null){
throw new JsonSerializationException(\\"Required property 'email' is missing\\");
}

value.Email = jo[\\"email\\"].ToObject<string>(serializer);

if(jo[\\"name\\"] != null) {
value.Name = jo[\\"name\\"].ToObject<string?>(serializer);
}


Expand All @@ -45,6 +58,10 @@ public class RootConverter : JsonConverter<Root>
{
jo.Add(\\"email\\", JToken.FromObject(value.Email, serializer));
}
if (value.Name != null)
{
jo.Add(\\"name\\", JToken.FromObject(value.Name, serializer));
}


jo.WriteTo(writer);
Expand Down
13 changes: 12 additions & 1 deletion examples/csharp-generate-newtonsoft-serializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@ import {
} from '../../src';

const generator = new CSharpGenerator({
presets: [CSHARP_NEWTONSOFT_SERIALIZER_PRESET]
presets: [
{
preset: CSHARP_NEWTONSOFT_SERIALIZER_PRESET,
options: {
enforceRequired: true
}
}
]
});

const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
required: ['email'],
properties: {
email: {
type: 'string',
format: 'email'
},
name: {
type: 'string'
}
}
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading