From 0cc0334a0055b18b96c54b765fa2d3351df01665 Mon Sep 17 00:00:00 2001 From: Richard Carruthers Date: Sun, 5 Oct 2025 08:49:42 -0500 Subject: [PATCH 1/2] feat: add ToString/toString/str serialization methods to TonDocument Implement convenience serialization methods across all language implementations: - C#: Override ToString() to serialize using TonSerializer with default options - JavaScript: Update toString() to use TonSerializer instead of JSON.stringify - Python: Update __str__() to use TonSerializer instead of json.dumps Updated README examples to demonstrate: - Quick serialization using ToString()/toString()/str() - Explicit TonSerializer usage for more control over serialization options This provides a more intuitive API for users who want to quickly serialize TON documents without explicitly creating a serializer instance. --- README.md | 24 ++++++++++++++----- .../DevPossible.Ton/src/Models/TonDocument.cs | 12 +++++++++- .../devpossible-ton/src/models/TonDocument.ts | 7 ++++-- .../devpossible_ton/models/ton_document.py | 6 +++-- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e1ceae7..9284249 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,13 @@ var database = new TonObject { ClassName = "database" }; database.SetProperty("host", TonValue.From("localhost")); newDoc.RootObject.AddChild(database); -// Serialize and save +// Serialize - using ToString() for quick serialization with default options +string tonContent = newDoc.ToString(); +Console.WriteLine(tonContent); + +// Or use TonSerializer for more control var serializer = new TonSerializer(); -string tonContent = serializer.SerializeDocument(newDoc, TonSerializeOptions.Pretty); +string prettyTon = serializer.SerializeDocument(newDoc, TonSerializeOptions.Pretty); await serializer.SerializeToFileAsync(newDoc, "config.ton", TonSerializeOptions.Pretty); ``` @@ -134,9 +138,13 @@ const database = new TonObject('database'); database.setProperty('host', TonValue.from('localhost')); newDoc.rootObject.addChild(database); -// Serialize and save +// Serialize - using toString() for quick serialization with default options +const tonContent = newDoc.toString(); +console.log(tonContent); + +// Or use TonSerializer for more control const serializer = new TonSerializer(); -const tonContent = serializer.serializeDocument(newDoc, TonSerializeOptions.Pretty); +const prettyTon = serializer.serializeDocument(newDoc, TonSerializeOptions.Pretty); await serializer.serializeToFile(newDoc, 'config.ton', TonSerializeOptions.Pretty); ``` @@ -165,9 +173,13 @@ database = TonObject(class_name='database') database.set_property('host', TonValue.from_value('localhost')) new_doc.root_object.add_child(database) -# Serialize and save +# Serialize - using str() for quick serialization with default options +ton_content = str(new_doc) +print(ton_content) + +# Or use TonSerializer for more control serializer = TonSerializer() -ton_content = serializer.serialize_document(new_doc, TonSerializeOptions.Pretty) +pretty_ton = serializer.serialize_document(new_doc, TonSerializeOptions.Pretty) await serializer.serialize_to_file(new_doc, 'config.ton', TonSerializeOptions.Pretty) ``` diff --git a/src/CSharp/DevPossible.Ton/src/Models/TonDocument.cs b/src/CSharp/DevPossible.Ton/src/Models/TonDocument.cs index 185f0c1..96ea801 100644 --- a/src/CSharp/DevPossible.Ton/src/Models/TonDocument.cs +++ b/src/CSharp/DevPossible.Ton/src/Models/TonDocument.cs @@ -111,6 +111,16 @@ public static TonDocument FromObject(object obj) var rootObject = TonObject.FromObject(obj); return new TonDocument(rootObject); } + + /// + /// Serializes the document to a TON string using default options + /// + /// The serialized TON document + public override string ToString() + { + var serializer = new TonSerializer(); + return serializer.Serialize(this); + } } /// @@ -142,4 +152,4 @@ public object? this[string key] set => Attributes[key] = value; } } -} \ No newline at end of file +} diff --git a/src/JavaScript/devpossible-ton/src/models/TonDocument.ts b/src/JavaScript/devpossible-ton/src/models/TonDocument.ts index 940ac2f..10c59be 100644 --- a/src/JavaScript/devpossible-ton/src/models/TonDocument.ts +++ b/src/JavaScript/devpossible-ton/src/models/TonDocument.ts @@ -136,7 +136,10 @@ export class TonDocument { } public toString(): string { - return JSON.stringify(this.toJSON(), null, 2); + // Import is handled at the top of the file if needed + const { TonSerializer } = require('../serializer/TonSerializer'); + const serializer = new TonSerializer(); + return serializer.serialize(this); } /** @@ -160,4 +163,4 @@ export class TonDocument { } return new TonDocument(new TonValue(obj)); } -} \ No newline at end of file +} diff --git a/src/Python/devpossible_ton/devpossible_ton/models/ton_document.py b/src/Python/devpossible_ton/devpossible_ton/models/ton_document.py index 7ef50f6..83d4a79 100644 --- a/src/Python/devpossible_ton/devpossible_ton/models/ton_document.py +++ b/src/Python/devpossible_ton/devpossible_ton/models/ton_document.py @@ -78,8 +78,10 @@ def to_json(self) -> Any: return self.root def __str__(self) -> str: - """String representation.""" - return json.dumps(self.to_json(), indent=2) + """String representation - serializes to TON format using default options.""" + from ..serializer.ton_serializer import TonSerializer + serializer = TonSerializer() + return serializer.serialize(self) # Monkey-patch json.dumps to use our encoder by default for TonDocument types From 6cb7031ff1f02a525b79442518f8a5f940a3aa2d Mon Sep 17 00:00:00 2001 From: Richard Carruthers Date: Sun, 5 Oct 2025 08:50:18 -0500 Subject: [PATCH 2/2] Version Bump --- src/CSharp/DevPossible.Ton/DevPossible.Ton.csproj | 5 +++-- src/CSharp/DevPossible.Ton/README.md | 3 ++- src/JavaScript/devpossible-ton/package.json | 2 +- src/Python/devpossible_ton/setup.py | 3 ++- version.json | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/CSharp/DevPossible.Ton/DevPossible.Ton.csproj b/src/CSharp/DevPossible.Ton/DevPossible.Ton.csproj index d4befdf..ff6c0e5 100644 --- a/src/CSharp/DevPossible.Ton/DevPossible.Ton.csproj +++ b/src/CSharp/DevPossible.Ton/DevPossible.Ton.csproj @@ -5,7 +5,7 @@ enable true DevPossible.Ton - 0.1.6 + 0.1.7 DevPossible, LLC DevPossible, LLC DevPossible.Ton @@ -27,7 +27,7 @@ Copyright © 2024 DevPossible, LLC 1.0.0.0 1.0.0.0 - 0.1.6 + 0.1.7 DevPossible, LLC @@ -40,3 +40,4 @@ + diff --git a/src/CSharp/DevPossible.Ton/README.md b/src/CSharp/DevPossible.Ton/README.md index 8e53dbd..7c6f9cb 100644 --- a/src/CSharp/DevPossible.Ton/README.md +++ b/src/CSharp/DevPossible.Ton/README.md @@ -22,7 +22,7 @@ Install-Package DevPossible.Ton ### Via PackageReference ```xml - + ``` ## Quick Start @@ -235,3 +235,4 @@ TON Specification: [tonspec.com](https://tonspec.com) **© 2024 DevPossible, LLC. All rights reserved.** + diff --git a/src/JavaScript/devpossible-ton/package.json b/src/JavaScript/devpossible-ton/package.json index 15983b9..20de345 100644 --- a/src/JavaScript/devpossible-ton/package.json +++ b/src/JavaScript/devpossible-ton/package.json @@ -1,6 +1,6 @@ { "name": "@devpossible/ton", - "version": "0.1.6", + "version": "0.1.7", "description": "JavaScript library for parsing, validating, and serializing TON (Text Object Notation) files. Full specification at https://tonspec.com. ALPHA RELEASE: Core functionality is complete but API may change before stable 1.0 release.", "type": "module", "main": "dist/index.js", diff --git a/src/Python/devpossible_ton/setup.py b/src/Python/devpossible_ton/setup.py index c9b4494..790df3d 100644 --- a/src/Python/devpossible_ton/setup.py +++ b/src/Python/devpossible_ton/setup.py @@ -10,7 +10,7 @@ setup( name="devpossible-ton", - version="0.1.6", + version="0.1.7", author="DevPossible, LLC", author_email="support@devpossible.com", description="Python library for parsing, validating, and serializing TON (Text Object Notation) files. Full specification at https://tonspec.com. ALPHA RELEASE: Core functionality is complete but API may change before stable 1.0 release.", @@ -66,3 +66,4 @@ + diff --git a/version.json b/version.json index 438a445..351b3f2 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "library_version": "0.1.6", + "library_version": "0.1.7", "ton_spec_version": "1.0", "description": "Centralized version file for all DevPossible.Ton packages. library_version is for the package, ton_spec_version is for the TON file format specification." }