C
C#16mo ago
Craluminum

How to write a specific script

I need a script that can turn this:
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
into this:
tf.Translation.X = -0.15f;
tf.Translation.Y = -0.22f;
tf.Translation.Z = 0;
tf.Rotation.X = 0;
tf.Rotation.Y = 45;
tf.Rotation.Z = 0;
tf.Origin.X = 0.8f;
tf.Origin.Y = 0;
tf.Origin.Z = 0.5f;
tf.Translation.X = -0.15f;
tf.Translation.Y = -0.22f;
tf.Translation.Z = 0;
tf.Rotation.X = 0;
tf.Rotation.Y = 45;
tf.Rotation.Z = 0;
tf.Origin.X = 0.8f;
tf.Origin.Y = 0;
tf.Origin.Z = 0.5f;
31 Replies
Craluminum
Craluminum16mo ago
The input format is JSON
Jimmacle
Jimmacle16mo ago
have you looked at what's available in System.Text.Json?
Craluminum
Craluminum16mo ago
Not yet What class can do that from object? Or from string
Jimmacle
Jimmacle16mo ago
you should google that namespace i mentioned
Henkypenky
Henkypenky16mo ago
$json
MODiX
MODiX16mo ago
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Craluminum
Craluminum16mo ago
I tried this
using System.Text.Json;

namespace CodeFromJson
{
public class ModelTransform
{
public Vec3f Translation;
public Vec3f Rotation;
public Vec3f Origin;
public float Scale;
}

public class Vec3f
{
public float X;
public float Y;
public float Z;
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<ModelTransform>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf?.Translation.X}");
Console.WriteLine($"tf.Translation.Y = {tf?.Translation.Y}");
Console.WriteLine($"tf.Translation.Z = {tf?.Translation.Z}");
Console.WriteLine($"tf.Origin.X = {tf?.Origin.X}");
Console.WriteLine($"tf.Origin.Y = {tf?.Origin.Y}");
Console.WriteLine($"tf.Origin.Z = {tf?.Origin.Z}");
Console.WriteLine($"tf.Rotation.X = {tf?.Rotation.X}");
Console.WriteLine($"tf.Rotation.Y = {tf?.Rotation.Y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.Rotation.Z}");
Console.WriteLine($"tf.Scale = {tf?.Scale}");
}
}
}
using System.Text.Json;

namespace CodeFromJson
{
public class ModelTransform
{
public Vec3f Translation;
public Vec3f Rotation;
public Vec3f Origin;
public float Scale;
}

public class Vec3f
{
public float X;
public float Y;
public float Z;
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<ModelTransform>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf?.Translation.X}");
Console.WriteLine($"tf.Translation.Y = {tf?.Translation.Y}");
Console.WriteLine($"tf.Translation.Z = {tf?.Translation.Z}");
Console.WriteLine($"tf.Origin.X = {tf?.Origin.X}");
Console.WriteLine($"tf.Origin.Y = {tf?.Origin.Y}");
Console.WriteLine($"tf.Origin.Z = {tf?.Origin.Z}");
Console.WriteLine($"tf.Rotation.X = {tf?.Rotation.X}");
Console.WriteLine($"tf.Rotation.Y = {tf?.Rotation.Y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.Rotation.Z}");
Console.WriteLine($"tf.Scale = {tf?.Scale}");
}
}
}
It doesn't work
Henkypenky
Henkypenky16mo ago
probably because this json
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
is invalid
Craluminum
Craluminum16mo ago
I input this line
"groundStorageTransform": { "rotation": { "x": 0, "y": 45, "z": 0 }, "origin": { "x": 0.5, "y": 0, "z": 0.5 }, "scale": 0.85 }
"groundStorageTransform": { "rotation": { "x": 0, "y": 45, "z": 0 }, "origin": { "x": 0.5, "y": 0, "z": 0.5 }, "scale": 0.85 }
Henkypenky
Henkypenky16mo ago
invalid
Craluminum
Craluminum16mo ago
Why? How I can fix it?
Henkypenky
Henkypenky16mo ago
{
"groundStorageTransform": {
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.5,
"y": 0,
"z": 0.5
},
"scale": 0.85
}
}
{
"groundStorageTransform": {
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.5,
"y": 0,
"z": 0.5
},
"scale": 0.85
}
}
Craluminum
Craluminum16mo ago
It is strange
Henkypenky
Henkypenky16mo ago
the problem is in your model this
Craluminum
Craluminum16mo ago
I need to add new()?
Henkypenky
Henkypenky16mo ago
{
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
}
{
"translation": {
"x": -0.15,
"y": -0.22,
"z": 0
},
"rotation": {
"x": 0,
"y": 45,
"z": 0
},
"origin": {
"x": 0.8,
"y": 0,
"z": 0.5
}
}
is this
public class Origin
{
public double x { get; set; }
public int y { get; set; }
public double z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}

public class Translation
{
public double x { get; set; }
public double y { get; set; }
public int z { get; set; }
}
public class Origin
{
public double x { get; set; }
public int y { get; set; }
public double z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}

public class Translation
{
public double x { get; set; }
public double y { get; set; }
public int z { get; set; }
}
Craluminum
Craluminum16mo ago
I need only float for numbers
Henkypenky
Henkypenky16mo ago
then get rid of the properties you don't need
Henkypenky
Henkypenky16mo ago
Henkypenky
Henkypenky16mo ago
using System.Text.Json;

namespace CodeFromJson
{
public class Origin
{
public double x { get; set; }
public int y { get; set; }
public double z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}

public class Translation
{
public double x { get; set; }
public double y { get; set; }
public int z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf.translation.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin.x}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation.z}");
}
}
}
using System.Text.Json;

namespace CodeFromJson
{
public class Origin
{
public double x { get; set; }
public int y { get; set; }
public double z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}

public class Translation
{
public double x { get; set; }
public double y { get; set; }
public int z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf.translation.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin.x}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation.z}");
}
}
}
you can create the vector from your values
Craluminum
Craluminum16mo ago
translation, origin and rotation are vectors
Henkypenky
Henkypenky16mo ago
no, they are not they are just kvp in a json they are numbers that you want to create a vector from those 3 numbers is another story
Craluminum
Craluminum16mo ago
I only need to output all numbers as float
Henkypenky
Henkypenky16mo ago
then change the type to float
using System.Text.Json;

namespace CodeFromJson
{
public class Origin
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Translation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf.translation.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin.x}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation.z}");
}
}
}
using System.Text.Json;

namespace CodeFromJson
{
public class Origin
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Rotation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Translation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString);

Console.WriteLine($"tf.Translation.X = {tf.translation.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin.x}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation.z}");
}
}
}
Craluminum
Craluminum16mo ago
I input valid json but it gives null for something I forgot to put null check at translation And still no success It outputs all values as null
Henkypenky
Henkypenky16mo ago
show code
Craluminum
Craluminum16mo ago
using System.Text.Json;

namespace CodeFromJson
{
public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Origin
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Rotation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Translation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString!);

Console.WriteLine($"tf.Translation.X = {tf?.translation?.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation?.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation?.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin?.z}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin?.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin?.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation?.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation?.z}");
}
}
}
using System.Text.Json;

namespace CodeFromJson
{
public class Root
{
public Translation translation { get; set; }
public Rotation rotation { get; set; }
public Origin origin { get; set; }
}

public class Origin
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Rotation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public class Translation
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}

public static class CodeFromJson
{
public static void Main(string[] args)
{
Console.WriteLine("JSON Input:");
string? jsonString = Console.ReadLine();
var tf = JsonSerializer.Deserialize<Root>(jsonString!);

Console.WriteLine($"tf.Translation.X = {tf?.translation?.x}");
Console.WriteLine($"tf.Translation.Y = {tf?.translation?.y}");
Console.WriteLine($"tf.Translation.Z = {tf?.translation?.z}");
Console.WriteLine($"tf.Origin.X = {tf?.origin?.z}");
Console.WriteLine($"tf.Origin.Y = {tf?.origin?.y}");
Console.WriteLine($"tf.Origin.Z = {tf?.origin?.z}");
Console.WriteLine($"tf.Rotation.X = {tf?.rotation.x}");
Console.WriteLine($"tf.Rotation.Y = {tf?.rotation?.y}");
Console.WriteLine($"tf.Rotation.Z = {tf?.rotation?.z}");
}
}
}
Henkypenky
Henkypenky16mo ago
works for me pass this json
{"translation": {"x": -0.15,"y": -0.22,"z": 0},"rotation": {"x": 0,"y": 45,"z": 0},"origin": {"x": 0.8,"y": 0,"z": 0.5}}
{"translation": {"x": -0.15,"y": -0.22,"z": 0},"rotation": {"x": 0,"y": 45,"z": 0},"origin": {"x": 0.8,"y": 0,"z": 0.5}}
Craluminum
Craluminum16mo ago
Yeah it works Thank you ❤️
Henkypenky
Henkypenky16mo ago
Ok
Craluminum
Craluminum16mo ago
I hope I make it as VSCode extension someday