JSON Serialization/Deserialization in FunctionGraph¶
This document describes how to use JSON serialization and deserialization
in FunctionGraph C# functions using the
OpenTelekomCloud.Serverless.Function.Common.JsonSerializer class.
In this example, we will demonstrate how to serialize and deserialize a simple object with a single property.
{
"Key": "Value"
}
Source¶
Source for this sample can be found in: samples-doc/serialization_simple.
C# project file¶
To use JSON serialization/deserialization, first add a reference to the OpenTelekomCloud.Serverless.Function.Common library in your project file:
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<ItemGroup>
<PackageReference Include="OpenTelekomCloud.Serverless.Function.Common" Version="*-*" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="OpenTelekomCloud.Serverless.Function.Common.JsonSerializer"
Version="*-*" />
</ItemGroup>
</Project>
C# program files¶
Define a simple class to represent the object structure of the JSON data:
/// <summary>
/// Define a class to be serialized/deserialized
/// </summary>
public class TestJson
{
//Define the attribute of the serialization class as Key.
public string Key { get; set; }
}
Implement the function handler to perform serialization and deserialization:
namespace src
{
#if NET6_0_OR_GREATER
using OpenTelekomCloud.Serverless.Function.Common;
#else
using HC.Serverless.Function.Common;
using OpenTelekomCloud.Serverless.Function.Common;
#endif
using System;
using System.IO;
using System.Text;
/// <summary>
/// FunctionGraph C# runtime program for JSON serialization/deserialization example
/// </summary>
public class Program
{
/// <summary>
/// Main method - not used in FunctionGraph but needed for compilation
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
Console.WriteLine("This is a FunctionGraph C# runtime program");
}
public Stream Handler(Stream input, IFunctionContext context)
{
var logger = context.Logger;
logger.Logf("Serialization sample CSharp");
JsonSerializer test = new JsonSerializer();
// Deserialize input JSON
TestJson testJson = test.Deserialize<TestJson>(input);
if (testJson != null)
{
logger.Logf("json Deserialize Key={0}", testJson.Key);
}
else
{
return null;
}
// Serialize output JSON
return test.Serialize<TestJson>(testJson);
}
}
}
Handler file¶
serialization_simple::src.Program::Handler
Build the project¶
To build the project, navigate to the project directory and run the following command:
dotnet build -c Release
This command builds the project for all target frameworks defined in the project file and creates a zip file for each target framework in the project folder.
Deploy the function¶
Use OpentelekomCloud FunctionGraph console to create a function with following settings:
Create function¶
Create With: Create from scratch
Basic Information
Function Type Event Function
Region <YOUR REGION>
Function Name <YOUR FUNCTION NAME>
Runtime C# (.NET 6.0)
Upload code¶
Use Upload > Local ZIP and upload serialization_simple_net6.0.zip from previous step.
Configure function¶
In Configuration > Basic Settings > Handler: set value to name as defined in handler.txt
Test the function¶
Create Test Event¶
In Code create a Test Event using “Blank Template” and add following content:
{
"Key": "Value"
}
Test function¶
Click Test to test function.
The function execution result is displayed in the Execution Result section.