TooString 0.3.0-preview

This is a prerelease version of TooString.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TooString --version 0.3.0-preview
                    
NuGet\Install-Package TooString -Version 0.3.0-preview
                    
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.3.0-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TooString" Version="0.3.0-preview" />
                    
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.3.0-preview
                    
#r "nuget: TooString, 0.3.0-preview"
                    
#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.
#addin nuget:?package=TooString&version=0.3.0-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TooString&version=0.3.0-preview&prerelease
                    
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 ‘debug view’ style, or as [CallerArgumentExpression]

TooString offers 3 extension method groups on Object:

value.TooString();
value.ToJson();
value.ToDebugViewString();

TooString is not a serializer, it is intended for test and diagnostic display. A reliable Serializer must be fail-fast — it should throw if it cannot deterministically serialize the input — but TooString is best effort; it will attempt to return a partial representation of the input even when input cannot reliably be serialized.

Example:

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

var anonObject = new { A = "boo", B = new Complex(3,4) };
anonObject.ToJson();
anonObject.TooString(TooStringHow.Json);
// Output is the System.Text.Json output:
// {"A":"boo","B":{"Real":3,"Imaginary":4,"Magnitude":5,"Phase":0.9272952180016122}}

anonObject.ToDebugViewString();
anonObject.TooString(TooStringHow.Reflection);
// Output is "{ A = boo, B = (3, 4) }" 

var tuple = (one: 1, two: "2", three: new Complex(3,4));
System.Text.Json.JsonSerializer.Serialize(tuple)
// Output is "{}"

tuple.ToJson()
tuple.TooString()
tuple.TooString(TooStringHow.Json)
// Output is created by reflection and presents the tuple and the Complex number as arrays
// [1,"2",[3,4]] 

tuple.ToDebugViewString()
tuple.TooString(TooStringHow.Reflection)
// Output is created by reflection and mimics typical debugger display
// on Net6.0: {item1 = 1, item2 = "2", item3 = (3,4)}  
// on Net8.0: {item1 = 1, item2 = "2", item3 = <3;4>}

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.

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

// do this instead:

var options = TooStringOptions.Default with
{
    JsonOptions = new JsonSerializerOptions { IncludeFields = true }
};
var jsonnedIncludeFields = (one:1, two:"2") .TooString(options);
// Output is "{"Item1":1,"Item2":"2"}"

- Infinite loops are avoided with MaxDepth settings.

ChangeLog

<pre> 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 was computed.  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.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 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.Action() .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() .ShouldEqualByValue().ShouldEqualByValueExceptFor(...).ShouldEqualByValueOnMembers() work with all kinds of object and collections, and report what differed. string.ShouldMatch(pattern).ShouldNotMatch().ShouldBeEmpty().ShouldNotBeEmpty() .ShouldNotBeNullOrEmptyOrWhiteSpace().ShouldEqualIgnoringCase() .ShouldContain().ShouldStartWith().ShouldEndWith().ShouldBeContainedIn(), ... 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(); ```

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0-preview 205 6/24/2025
0.3.0-preview 155 6/24/2025
0.2.0-preview 126 6/20/2025
0.1.0 1,789 7/11/2024

ChangeLog
           ---------
           0.3.0  ReflectionOptiojns.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.