Unable to import TimeSpan from string

13 views
Skip to first unread message

Bruno Tezine

unread,
Jun 11, 2015, 10:43:16 AM6/11/15
to jay...@googlegroups.com
Hi, 
I'm trying to import an object that contains a TimeSpan field (as string). 
I already created a TimeSpanImporter class with the function ImportFromString...
The function is called correctly and the TimeSpan is created fine, but after that I get an exception {"Found String where Member was expected."}. 

Here is the object: 
    public class ETest {
        public TimeSpan tm { get; set; }
    }


The string sent: 
"{\"tm\":\"11:28:47\"}"


The overrided function:
protected override object ImportFromString(ImportContext context, JsonReader reader){
            try {
                TimeSpan tm;
                bool ok=TimeSpan.TryParse(reader.Text, out tm);
                if (!ok) tm=(TimeSpan) base.ImportFromString(context, reader);               
                return tm;
            } catch (Exception ex) {
                SLogger.Error(this, ex.ToString());                
            }
            return null;
        }


The function that calls the import:
static public testImport(string txt){
  try{
    JsonConvert.Import(parameter.ParameterType, txt);// txt="{\"tm\":\"11:28:47\"}"
  }catch(Exception ex){
     //THIS EXCEPTION IS FIRED WITH MESSAGE: {"Found String where Member was expected."}. 
 }
}

What am I missing? 
Thank you, 
    Bruno.

Atif Aziz

unread,
Jun 11, 2015, 12:37:52 PM6/11/15
to jay...@googlegroups.com
Your problem is that you forgot to advance the reader. See the following gist for the fix:

- Atif
--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jayrock+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruno Tezine

unread,
Jun 11, 2015, 4:54:33 PM6/11/15
to jay...@googlegroups.com
Thank you Atif! 
You saved my life! 
Reply all
Reply to author
Forward
0 new messages