@@ -42,7 +42,12 @@ protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarV
4242 {
4343 if ( scalar == null )
4444 throw new ArgumentNullException ( nameof ( scalar ) ) ;
45- return FormatLiteralValue ( scalar , state . Output , state . Format ) ;
45+
46+ // At the top level, for scalar values, use "display" rendering.
47+ if ( state . IsTopLevel )
48+ return _displayFormatter . FormatLiteralValue ( scalar , state . Output , state . Format ) ;
49+
50+ return FormatLiteralValue ( scalar , state . Output ) ;
4651 }
4752
4853 protected override int VisitSequenceValue ( ThemedValueFormatterState state , SequenceValue sequence )
@@ -63,7 +68,7 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque
6368 state . Output . Write ( delim ) ;
6469
6570 delim = ", " ;
66- Visit ( state , sequence . Elements [ index ] ) ;
71+ Visit ( state . Nest ( ) , sequence . Elements [ index ] ) ;
6772 }
6873
6974 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
@@ -96,8 +101,9 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
96101 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
97102 state . Output . Write ( ": " ) ;
98103
99- count += Visit ( state , property . Value ) ;
104+ count += Visit ( state . Nest ( ) , property . Value ) ;
100105 }
106+
101107 if ( structure . TypeTag != null )
102108 {
103109 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
@@ -121,7 +127,7 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
121127
122128 protected override int VisitDictionaryValue ( ThemedValueFormatterState state , DictionaryValue dictionary )
123129 {
124- int count = 0 ;
130+ var count = 0 ;
125131
126132 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
127133 state . Output . Write ( '{' ) ;
@@ -135,13 +141,19 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
135141
136142 delim = ", " ;
137143
138- using ( ApplyStyle ( state . Output , ConsoleThemeStyle . String , ref count ) )
144+ var style = element . Key . Value == null
145+ ? ConsoleThemeStyle . Null
146+ : element . Key . Value is string
147+ ? ConsoleThemeStyle . String
148+ : ConsoleThemeStyle . Scalar ;
149+
150+ using ( ApplyStyle ( state . Output , style , ref count ) )
139151 JsonValueFormatter . WriteQuotedJsonString ( ( element . Key . Value ?? "null" ) . ToString ( ) , state . Output ) ;
140152
141153 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
142154 state . Output . Write ( ": " ) ;
143155
144- count += Visit ( state , element . Value ) ;
156+ count += Visit ( state . Nest ( ) , element . Value ) ;
145157 }
146158
147159 using ( ApplyStyle ( state . Output , ConsoleThemeStyle . TertiaryText , ref count ) )
@@ -150,12 +162,8 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
150162 return count ;
151163 }
152164
153- int FormatLiteralValue ( ScalarValue scalar , TextWriter output , string format )
165+ int FormatLiteralValue ( ScalarValue scalar , TextWriter output )
154166 {
155- // At the top level, if a format string is specified, non-JSON rendering is used.
156- if ( format != null )
157- return _displayFormatter . FormatLiteralValue ( scalar , output , format ) ;
158-
159167 var value = scalar . Value ;
160168 var count = 0 ;
161169
@@ -175,7 +183,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
175183
176184 if ( value is ValueType )
177185 {
178- if ( value is int || value is uint || value is long || value is ulong || value is decimal || value is byte || ( value is sbyte || value is short ) || value is ushort )
186+ if ( value is int || value is uint || value is long || value is ulong || value is decimal || value is byte || value is sbyte || value is short || value is ushort )
179187 {
180188 using ( ApplyStyle ( output , ConsoleThemeStyle . Number , ref count ) )
181189 output . Write ( ( ( IFormattable ) value ) . ToString ( null , CultureInfo . InvariantCulture ) ) ;
@@ -184,23 +192,25 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
184192
185193 if ( value is double d )
186194 {
187- if ( double . IsNaN ( d ) || double . IsInfinity ( d ) )
188- using ( ApplyStyle ( output , ConsoleThemeStyle . String , ref count ) )
195+ using ( ApplyStyle ( output , ConsoleThemeStyle . Number , ref count ) )
196+ {
197+ if ( double . IsNaN ( d ) || double . IsInfinity ( d ) )
189198 JsonValueFormatter . WriteQuotedJsonString ( d . ToString ( CultureInfo . InvariantCulture ) , output ) ;
190- else
191- using ( ApplyStyle ( output , ConsoleThemeStyle . Number , ref count ) )
199+ else
192200 output . Write ( d . ToString ( "R" , CultureInfo . InvariantCulture ) ) ;
201+ }
193202 return count ;
194203 }
195204
196205 if ( value is float f )
197206 {
198- if ( double . IsNaN ( f ) || double . IsInfinity ( f ) )
199- using ( ApplyStyle ( output , ConsoleThemeStyle . String , ref count ) )
207+ using ( ApplyStyle ( output , ConsoleThemeStyle . Number , ref count ) )
208+ {
209+ if ( double . IsNaN ( f ) || double . IsInfinity ( f ) )
200210 JsonValueFormatter . WriteQuotedJsonString ( f . ToString ( CultureInfo . InvariantCulture ) , output ) ;
201- else
202- using ( ApplyStyle ( output , ConsoleThemeStyle . Number , ref count ) )
211+ else
203212 output . Write ( f . ToString ( "R" , CultureInfo . InvariantCulture ) ) ;
213+ }
204214 return count ;
205215 }
206216
@@ -214,14 +224,14 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
214224
215225 if ( value is char ch )
216226 {
217- using ( ApplyStyle ( output , ConsoleThemeStyle . String , ref count ) )
227+ using ( ApplyStyle ( output , ConsoleThemeStyle . Scalar , ref count ) )
218228 JsonValueFormatter . WriteQuotedJsonString ( ch . ToString ( ) , output ) ;
219229 return count ;
220230 }
221231
222232 if ( value is DateTime || value is DateTimeOffset )
223233 {
224- using ( ApplyStyle ( output , ConsoleThemeStyle . String , ref count ) )
234+ using ( ApplyStyle ( output , ConsoleThemeStyle . Scalar , ref count ) )
225235 {
226236 output . Write ( '"' ) ;
227237 output . Write ( ( ( IFormattable ) value ) . ToString ( "O" , CultureInfo . InvariantCulture ) ) ;
@@ -231,7 +241,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
231241 }
232242 }
233243
234- using ( ApplyStyle ( output , ConsoleThemeStyle . String , ref count ) )
244+ using ( ApplyStyle ( output , ConsoleThemeStyle . Scalar , ref count ) )
235245 JsonValueFormatter . WriteQuotedJsonString ( value . ToString ( ) , output ) ;
236246
237247 return count ;
0 commit comments