c# - JavaScriptSerializer and ValueTypes (struct) -


for project i've created several struct in c#. probject asp.net mvc 2 project.

snip:

struct tdummy {     private char _value;      public tdummy(char value)     {          this._value = value; // restrictions     } } 

i created because needed restrict char-variable specific number of values. (i have created enum, these values used in database, , still need convert them)

now need create jsonresult, like

return json(new { value = new tdummy('x') }); 

but when this, result of:

{"value":{}} 

i expected result of

{"value":"x"} 

i've tried several things, typeconverter (canconvertto(string)), custom type serializer (javascriptserializer.registerconverters()), either don't work or must return 'complex' json-object.

{"value":{"name":"value"}} 

any thoughts on this?
want serialize value-type value...

if interested, i've create small demo (console app) illustrate this:

public struct tstate {    public static tstate active = new tstate('a');    public static tstate pending = new tstate('p');     private char _value;     public tstate(char value)    {       switch (value)       {          case 'a':          case 'p':             this._value = value;  // known values             break;          default:             this._value = 'a';    // default value             break;       }    }     public static implicit operator tstate(char value)    {       switch (value)       {          case 'a':             return tstate.active;          case 'p':             return tstate.pending;       }       throw new invalidcastexception();    }     public static implicit operator char(tstate value)    {       return value._value;    }     public override string tostring()    {       return this._value.tostring();    } }  public class test { public tstate value { get; set; } }  class program {    static void main(string[] args)    {       test o = new test();       o.value = 'p';               // char       char c = o.value;            // char       console.writeline(o.value);  // writes 'p'       console.writeline(c);        // writes 'p'        javascriptserializer jser = new javascriptserializer();       console.writeline(jser.serialize(o));  // writes '{"value":{}}'        console.readline();    } } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -