Created
April 7, 2022 22:30
-
-
Save rido-min/bb2afdd5dbf573100b107bbe0260b29d to your computer and use it in GitHub Desktop.
ValidatePayload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"@context": [ | |
"dtmi:dtdl:context;2", | |
"dtmi:iotcentral:context;2" | |
], | |
"@id": "dtmi:samplesv2:geoTypes;1", | |
"@type": "Interface", | |
"contents": [ | |
{ | |
"@type": "Telemetry", | |
"name": "myPoint", | |
"schema": "point" | |
}, | |
{ | |
"@type": ["Telemetry", "Location"], | |
"name": "myGeopoint", | |
"schema": "geopoint" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.DigitalTwins.Parser; | |
using Microsoft.Azure.DigitalTwins.Parser.Models; | |
using System.Text.Json; | |
string basePath = Path.Join(System.Reflection.Assembly.GetExecutingAssembly().Location + @"./../../../"); | |
string ReadFile(string path) => File.ReadAllText(Path.Join(basePath, path)); | |
string ToJs(object obj) => JsonSerializer.Serialize(obj); | |
var objectModel = new ModelParser().Parse(new string[] { ReadFile("geotypes-1.json") }); | |
var rootInterface = (DTInterfaceInfo)objectModel[new Dtmi("dtmi:samplesv2:geoTypes;1")]; | |
var myPoint = (DTTelemetryInfo)rootInterface.Contents["myPoint"]; | |
var validPayload = myPoint.Schema.ValidateInstance(ToJs(new { type = "Point", coordinates = new double[] { 12.35, 23.56} })); | |
Console.WriteLine(validPayload); | |
var myGeopoint = (DTTelemetryInfo)rootInterface.Contents["myGeopoint"]; | |
var validPayload2 = myGeopoint.Schema.ValidateInstance(ToJs(new { lat =12.35, lon = 23.56 })); | |
Console.WriteLine(validPayload2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<RootNamespace>validate_payload</RootNamespace> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.DigitalTwins.Parser" Version="6.1.0-preview" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment