why does c# JavaScriptSerializer.Serialize return empty square brackets
Asked Answered
G

2

6

Why does the following code return "[ ]" when it should return "{"id":1999, "title":"hithere"}

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    StringBuilder sbJsonResults = new StringBuilder();
    var result = serializer.Serialize(new dg(1999, "hithere"));

    context.Response.Clear();
    context.Response.ContentType = "application/json; charset=utf-8";
    context.Response.Cache.SetExpires(DateTime.MinValue);
    context.Response.Write(result);

P.S. the dg class looks like this:

    public class dg : ScheduleObserver, ILibrary, IEnumerable {
        public int id;
        public string title;
        private List<d> dList;
        ...many getters and setters and some logic functions...
    }

    public abstract class ScheduleObserver{
        public abstract void update();
    }

    public interface ILibrary {
        List<PD> getPDs();
        void setPDs(List<PD> newPDs);
        int getCurrentIndex();
        void addPD(PD pD);
        PD getPD(int index);
    }

Many thanks.

THANKS - ANSWERED SUCCESSFULLY - it was the IEnumerable that was the source of my woes. To solve it, dg no longer extends IEnumerable and all foreach (dg...) loops have been converted back into for(int i = 0...) loops.

THANKS VERY, VERY MUCH! JAMES got why its empty, Parv got why had square brackets. Would mark both as answer but can only mark one.

Grajeda answered 15/4, 2013 at 12:32 Comment(5)
dg=[serializable]?Eyre
does dg have a constructor that matches the overload in your example and does that overload properly assigns values to corresponding properties?Dustydusza
@Brad - I get the same result with or without [Serializable] tag on dgGrajeda
@Dmitri - yes, has constructor, and can step through in debug to see dg object is correct (not same code as above but can be done)Grajeda
You haven't posted the implementation of ScheduleObserver but from what I can see you don't have any public setters/getters.Phytoplankton
P
4

Looking at the source the problem would appear to be a combination of a couple of things.

As @Parv has already pointed out the reason you get the [] is because your class implements IEnumerable so the serializer attempts to iterate the object and then serialize each item independently. Your current design is not going to work as it isn't designed to serialize public properties for types that implement IEnumerable.

The other issue, like I mentioned in a comment, is the your class doesn't appear to have any public properties, what you have at the moment are public variables. In order for the serializer to work you need property setter/getters i.e.

public class dg
{
    public int id { get; set; }
    public string title { get; set; }
    ...
}
Phytoplankton answered 15/4, 2013 at 13:33 Comment(0)
C
3

The problem might be that because the dg class inherits IEnumerable, the JavaScriptSerializer is treating it as an Array and hence the square brackets.

Try doing

var result = serializer.Serialize(
    new dg(1999, "hithere").Select(d => new{ id = d.id, title = d.title }));
Clavus answered 15/4, 2013 at 12:54 Comment(2)
@Phytoplankton IEnumerable is an Implemented Interface for Type dgClavus
very cool...And I was only using ienumerable for foreach loopsGrajeda

© 2022 - 2024 — McMap. All rights reserved.