TooString 0.6.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TooString --version 0.6.0
                    
NuGet\Install-Package TooString -Version 0.6.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TooString" Version="0.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TooString" Version="0.6.0" />
                    
Directory.Packages.props
<PackageReference Include="TooString" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TooString --version 0.6.0
                    
#r "nuget: TooString, 0.6.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TooString@0.6.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TooString&version=0.6.0
                    
Install as a Cake Addin
#tool nuget:?package=TooString&version=0.6.0
                    
Install as a Cake Tool

TooString is a Stringifier that goes places serializers don't.

TooString can

  • make a best effort to stringify objects that JsonSerializer won't, including System.Reflection classes and System.Type, or that JsonSerializer surprises you with, such as ValueTuples.
  • Output as Json or C# object notation, or ‘debug view’ style, or [CallerArgumentExpression]

TooString offers 3 extension method groups on Object:

value.TooString();
value.ToJson();
value.ToCallerArgumentString();

TooString is not a serializer.

  • A serializer should be fail-fast, but TooString is best-effort.
  • A Serializer should throw if it cannot deterministically serialize the input, but TooString will attempt to return a partial or alternative representation of the input if the input cannot be reliably serialized.
  • TooString offers both MaxDepth and MaxEnumerationLength options for abbreviated output.
Default behaviour
  • ToJson() or TooString(TooStringStyle.JsonStringifier) default to using System.Text.Json, falling back to reflection for un-serializable types.

whereas

  • TooString()
  • TooString(TooStringStyle.JsonStringifier)
  • TooString(TooStringStyle.CSharp)
  • TooString(TooStringStyle.DebugView) all defaults to MaxDepth = 4, MaxEnumerationLength = 9.

Example:

var value = new { A = "boo", B = new Complex(3,4) };

value.ToJson();       // Output Is System.Text.Json {"A":"boo","B":{"Real":3,"Imaginary":...}}
value.TooString(),    //Output is { A = "boo", B = <3; 4> } depending on .Net version.

( Math.Sqrt(4 * Math.PI / 3)  ).ToArgumentExpression() 
// Output is the literal code: "Math.Sqrt(4 * Math.PI / 3)"

var tuple = (one: 1, two: "2", three: new Complex(3,4));

System.Text.Json.JsonSerializer.Serialize(tuple) // Output is "{}" because there  
                                                 // are no public properties on a tuple

tuple.TooString(TooStringStyle.Json)
// Output stringifies the tuple and the Complex number as arrays
// [1,"2",[3,4]] 

tuple.TooString()
tuple.TooString(TooStringStyle.CSharp)
// Output is created by reflection and 
// on Net6.0: {item1 = 1, item2 = "2", item3 = (3,4)}  
// on Net8.0: {item1 = 1, item2 = "2", item3 = <3;4>}

var type = value.GetType();
System.Text.Json.JsonSerializer.Serialize(type) // Throws NotSupportedException

type.ToJson() // Outputs truncated Json with default MaxDepth = 4, MaxEnumerationLength = 9

Options

Options are finicky because we can do

  • Json serialization using System.Text.Json,

  • or Reflection-based stringification with

    • Json output or with
    • CSharp output,
    • or with DebugView output,
      • these all having MaxDepth, MaxEnumerationLength, and time/date format options.
  • or CallerArgumentExpression.

  • For Json serialization, the options are System.Text.Json.JsonSerializationOptions.

  • For stringifying, there are options for MaxDepth, MaxEnumerationLength (for enumerables), and for DateTime, DateOnly, TimeOnly, and TimeSpan formatting.

Try one of these approaches:

// For Json Output
value.ToJson()
value.TooString(TooStringStyle.JsonSerializer)
value.TooString( TooStringStyle.JsonStringifier )
value.TooString( AdvancedOptions.ForJson with { MaxEnumerationLength = 9 } )


// For CSharp objects,
value.TooString( TooStringStyle.CSharp)
value.TooString( AdvancedOptions.ForCSharp.With(...) )
value.TooString( maxDepth:4, maxLength:9, style:TooStringStyle.CSharp )
value.TooString( AdvancedOptions.ForStringified with 
{
    DateTimeFormat = "yyyyMMdd HH:mm:ss",
    TimeSpanFormat = @"d\.hh\:mm\:ss"
})

Gotchas

Example: Json-serializing value tuples is something of a surprise because (unlike anonymous objects or records or structs) they have no public properties and their public fields are not named as per your code. Takeaway: don't choose value tuples for public apis that must be jsonned.

Use modifications of TooStringOptions.Default to customise the results.

System.Text.Json.JsonSerializer.Serialize(  (one:1, two:"2")  )
// Output is "{}" because there are no public fields

(one:1, two:"2").TooString( TooStringHow.Json )
// Output is [1,"2"]. 
// The value tuple is detected as an ITuple, and we use reflection instead

ChangeLog

<pre> ChangeLog

0.5.0 ReflectionOptions.With(...), TooStringOptions.With(...). Fixes. 0.4.0 ReflectionOptions.MaxLength default = 9 ReflectionOptions.ForJson and ReflectionOptions.ForDebugView instead of Default. More overloads. 0.3.0 ReflectionOptions.MaxLength limits display of enumerable elements 0.2.0 Added Net8 (NB Net8 Json and Numerics output is different from Net6) Rename TooStringStyle to TooStringHow. Fix SerializationStyle.Reflection output of DateTime, DateOnly, TimeOnly. 0.1.0 Can use DebugView, Json, ToString() or [CallerArgumentExpression] and can output Json or Debug strings. </pre>

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on TooString:

Package Downloads
TestBase

*TestBase* gives you a flying start with - fluent assertions that are easy to extend - sharp error messages - tools to help you test with “heavyweight” dependencies on - AspNetCore.Mvc, AspNet.Mvc or WebApi Contexts - HttpClient - Ado.Net - Streams & Logging - Mix & match with your favourite test runners & assertions. ``` UnitUnderTest.Method() .ShouldNotBeNull() .ShouldEqualByValueExceptFor(new {Id=1, Descr=expected}, ignoreList ) .Payload .ShouldMatchIgnoringCase("I expected this") .Should(someOtherPredicate); .Items .ShouldAll(predicate) .ShouldContain(item) .ShouldNotContain(predicate) .Where(predicate) .SingleOrAssertFail() item .ShouldEqualByValue() .ShouldEqualByValueExceptFor(...) .ShouldEqualByValueOnMembers() string .ShouldMatch(pattern) .ShouldNotMatch() .ShouldBeEmpty() .ShouldNotBeEmpty() .ShouldNotBeNullOrEmptyOrWhiteSpace() .ShouldEqualIgnoringCase() .ShouldContain() .ShouldBeContainedIn() .ShouldStartWith() .ShouldEndWith() ... numeric .ShouldBeBetween() .ShouldEqualWithTolerance()....GreaterThan....LessThan...GreaterOrEqualTo ... ienumerable .ShouldAll() .ShouldContain() .ShouldNotContain() .ShouldBeEmpty() .ShouldNotBeEmpty() ... stream .ShouldHaveSameStreamContentAs() .ShouldContain() value .ShouldBe() .ShouldNotBe() .ShouldBeOfType() .ShouldBeAssignableTo() ... ``` Testable Logging is in packages Extensions.Logging.ListOfString and Serilog.Sinks.ListOfString ``` // Extensions.Logging.ListOfString var log = new List<String>(); ILogger mslogger= new LoggerFactory().AddStringListLogger(log).CreateLogger("Test2"); // Serilog.Sinks.ListOfString Serilog.Logger slogger= new LoggerConfiguration().WriteTo.StringList(log).CreateLogger(); ```

TestBase.Differ

TestBase.Differ provides deep comparison of objects, collections and strings, with human readable output options. ```csharp var result = Differ.Diff(expected, actual, options); if (!result) // Implicit bool conversion { Console.WriteLine(result.ToString()); } ```

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.0-preview 68 4/1/2026
0.6.0 124 3/29/2026
0.5.1-preview 249 3/26/2026
0.5.0-preview 222 6/30/2025
0.4.0-preview 4,005 6/24/2025
0.3.0-preview 258 6/24/2025
0.2.0-preview 228 6/20/2025
0.1.0 2,531 7/11/2024

ChangeLog
           ---------
           0.6.0  TooString() defaults to CSharp.
                  Add TooStringOptions.WriteIndented property.
                  Delete redundant styles.
           0.5.1  ReflectionOptions.Json and JsonOptions.WriteIndented output matchs STJ indentation. | Added net10.0
           0.5.0  ReflectionOptions.MaxEnumerationLength renamed from MaxLength | Options.With(...) methods for easy configuration
           0.4.0  ReflectionOptions.MaxLength default = 9 | ReflectionOptions.ForJson and ReflectionOptions.ForDebugView instead of Default | More overloads.
           0.3.0  ReflectionOptions.MaxLength limits display of enumerable elements
           0.2.0  Added Net8 (NB Net8 Json and Numerics output is different from Net6)
                  Rename TooStringStyle to TooStringHow.
                  Fix SerializationStyle.Reflection output of DateTime, DateOnly, TimeOnly.
           0.1.0  Can use DebugView, Json, ToString() or [CallerArgumentExpression] and can output Json or Debug strings.