1818// Use this define to diagnose issues in the JSON reader below.
1919//#define DEBUG_JSON_READER
2020
21+ using Microsoft . Xna . Framework ;
22+ using Newtonsoft . Json ;
2123using System ;
2224using System . Collections . Generic ;
2325using System . Diagnostics ;
2426using System . IO ;
2527using System . Linq ;
2628using System . Text ;
27- using Newtonsoft . Json ;
2829
2930namespace Orts . Parsers . OR
3031{
@@ -50,6 +51,7 @@ public static void ReadFile(string fileName, Func<JsonReader, bool> tryParse)
5051 JsonTextReader _reader ;
5152 StringBuilder _path ;
5253 Stack < int > _pathPositions ;
54+ int _countWarnings ;
5355
5456 /// <summary>
5557 /// Contains a condensed account of the position of the current item in the JSO, such as when parsing "Clear" from a WeatherFile:
@@ -71,6 +73,12 @@ public static void ReadFile(string fileName, Func<JsonReader, bool> tryParse)
7173 _pathPositions = new Stack < int > ( ) ;
7274 }
7375
76+ /// <summary>
77+ /// Reads next token and stores in _reader.TokenType, _reader.ValueType, _reader.Value
78+ /// Throws exception if value not as expected.
79+ /// PropertyNames are case-sensitive.
80+ /// </summary>
81+ /// <param name="tryParse"></param>
7482 public void ReadBlock ( Func < JsonReader , bool > tryParse )
7583 {
7684 var basePosition = _pathPositions . Count > 0 ? _pathPositions . Peek ( ) : 0 ;
@@ -114,6 +122,7 @@ public void ReadBlock(Func<JsonReader, bool> tryParse)
114122 switch ( _reader . TokenType )
115123 {
116124 case JsonToken . StartObject :
125+ case JsonToken . StartArray :
117126 case JsonToken . Boolean :
118127 case JsonToken . Bytes :
119128 case JsonToken . Date :
@@ -128,6 +137,13 @@ public void ReadBlock(Func<JsonReader, bool> tryParse)
128137 }
129138 }
130139
140+ public bool TryRead < T > ( Func < JsonReader , T > read , out T output )
141+ {
142+ var warnings = _countWarnings ;
143+ output = read ( this ) ;
144+ return warnings == _countWarnings ;
145+ }
146+
131147 public T AsEnum < T > ( T defaultValue )
132148 {
133149 Debug . Assert ( typeof ( T ) . IsEnum , "Must use type inheriting from Enum for AsEnum()" ) ;
@@ -168,6 +184,18 @@ public int AsInteger(int defaultValue)
168184 }
169185 }
170186
187+ public bool AsBoolean ( bool defaultValue )
188+ {
189+ switch ( _reader . TokenType )
190+ {
191+ case JsonToken . Boolean :
192+ return ( bool ) _reader . Value ;
193+ default :
194+ TraceWarning ( $ "Expected Boolean value in { Path } ; got { _reader . TokenType } ") ;
195+ return defaultValue ;
196+ }
197+ }
198+
171199 public string AsString ( string defaultValue )
172200 {
173201 switch ( _reader . TokenType )
@@ -194,9 +222,36 @@ public float AsTime(float defaultValue)
194222 }
195223 }
196224
225+ public Vector3 AsVector3 ( Vector3 defaultValue )
226+ {
227+ var vector3 = defaultValue ;
228+ switch ( _reader . TokenType )
229+ {
230+ case JsonToken . StartArray :
231+ if ( _reader . Read ( ) )
232+ vector3 . X = AsFloat ( 0f ) ;
233+ if ( _reader . Read ( ) )
234+ vector3 . Y = AsFloat ( 0f ) ;
235+ if ( _reader . Read ( ) )
236+ vector3 . Z = AsFloat ( 0f ) ;
237+ if ( ! _reader . Read ( ) || _reader . TokenType != JsonToken . EndArray )
238+ goto default ; // We did not have exactly 3 items in the array
239+ _path . Length = _pathPositions . Pop ( ) ;
240+ return vector3 ;
241+ default :
242+ TraceWarning ( $ "Expected Vector3 (3 item array) in { Path } ; got { _reader . TokenType } ") ;
243+
244+ // If the end of the array is not found in the right position, then parsing of subsequence objects also fails.
245+ TraceWarning ( $ "Subsequent objects may be skipped") ;
246+
247+ return defaultValue ;
248+ }
249+ }
250+
197251 public void TraceWarning ( string message )
198252 {
199253 Trace . TraceWarning ( "{2} in {0}:line {1}" , _fileName , _reader . LineNumber , message ) ;
254+ _countWarnings ++ ;
200255 }
201256
202257 public void TraceInformation ( string message )
0 commit comments