|
|
Comments and Discussions
|
|
 |

|
If id is a string then you shouldn't have a problem, but if it is an int then you can't put "1234,12345" in it.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Would it be possible to update the version submitted to NuGet? It's 1.9.6 at the moment and you have fixed a number of bugs between then and now.
Kind thanks,
Roja
|
|
|
|

|
Concerning objects implementing IList or ICollection, all properties (other than items) are not serialized.
This snippet of serializer's code, seems to confirm this fact :
else if (obj is Array || obj is IList || obj is ICollection)
WriteArray((IEnumerable)obj);
Is it a normal behaviour ?
|
|
|
|

|
I will look into this for the next release.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Thank you Mehdi for the quick response.
I have made the changes, it made the code simpler.
|
|
|
|

|
Also, don't neglect SortedList.
ATM it doesn't handle those too well.
Serialization mapping is partial and Deserialization throws exception for unknown type.
Also, given that both key-type and value-type of collection items could be object, the only way you could make deserialization work all the time is by inserting their [full type name] into the JSON string.
You can try all you want, but you're not gonna dodge this one and have a fail-safe product.
g
modified 10 Feb '13 - 1:44.
|
|
|
|

|
the only way you could make deserialization work all the time is by inserting their [full type name] into the JSON string
Not so sure, since deserialization can be done with a "target type" which have the needed full descriptor.
However, the embedded objects within fields/properties of the "target type" could be deserialized only according to the type of those fields/properties. So, if you put in a field/property of "object" type, an instance of a "xxx" type, deserializer have no way to guess the "xxx" type.
|
|
|
|

|
Hi,
I have a test-case which success to serialize, but fail to deserialize.
Here some code:
Class definitions:
[Serializable]
public class ConfigFormData
{
public Point Location { get; set; }
public Size Size { get; set; }
}
[Serializable]
public class ConfigData
{
public Dictionary<string, ConfigFormData> Forms { get; set; }
}
Usage:
ConfigData data = new ConfigData();
data.Forms = new Dictionary<string, ConfigFormData>();
data.Forms.Add(Name, new ConfigFormData(){Location = Location, Size = Size});
string json = fastJSON.JSON.Instance.ToJSON(data);
ConfigData data2;
data2 = fastJSON.JSON.Instance.ToObject<ConfigData>(json);
Json string:
{
"$types" : {
"WindowsFormsApplication2.Form1+ConfigData, WindowsFormsApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" : "1",
"WindowsFormsApplication2.Form1+ConfigFormData, WindowsFormsApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" : "2",
"System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" : "3",
"System.Drawing.Size, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" : "4"
},
"$type" : "1",
"Forms" : {
"Form1" : {
"$type" : "2",
"Location" : {
"$type" : "3",
"X" : 147,
"Y" : 147
},
"Size" : {
"$type" : "4",
"Width" : 384,
"Height" : 356
}
}
}
}
And the exception (in french, sorry):
Message: L'objet doit implémenter IConvertible.
Stack:
à System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
à fastJSON.JSON.ChangeType(Object value, Type conversionType)
à fastJSON.JSON.ParseDictionary(Dictionary`2 d, Dictionary`2 globaltypes, Type type, Object input)
à fastJSON.JSON.CreateStringKeyDictionary(Dictionary`2 reader, Type pt, Type[] types, Dictionary`2 globalTypes)
à fastJSON.JSON.ParseDictionary(Dictionary`2 d, Dictionary`2 globaltypes, Type type, Object input)
à fastJSON.JSON.ToObject(String json, Type type)
à fastJSON.JSON.ToObject[T](String json)
à WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) dans D:\tmp\WindowsFormsApplication2\Form1.cs:ligne 46
|
|
|
|

|
This should have been fixed in the current version, I will check again.
Thanks.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Hi, better later than never
I've check with the last version of FastJSON (2.0.13) and I still have the same exception. (same test case)
Sorry!
|
|
|
|

|
Point and Size (and other GDI types etc.) require the CustomType to be enabled as they are not supported by fastJSON ( you have to write de/serializers for them yourself).
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Hi Mehdi,
I'm very impressed with your implementation here. Particularly your performance results - the results against BinaryFormatter are very telling - and the fact that you beat Stack. I prototyped my own binary serializer a few years ago (never went into production), that was around 40x faster than the binary serializer, so I can appreciate a text-based format beating the binary formatter.
I actually needed fastJSON for it's ability to deserialize the more complex OO types. I can't believe that Stack resisted your requests. Although it would be good to call your solution a JSON and JSONOO serializer, for clarity.
Also, I have thoughts of re-visiting my binary formatter, now called eXtensible Binary eXchange (XBX). I particularly want to use it with Javascript over WebSockets. I would build it open source, and it would be tunable and configurable, with default setup for fulfilling most applications, but able to be tuned to minimize payload and maximize performance.
Please contact me if you would like more details and would like to help. I can share my draft Google Doc and we could work on the specification first, and possibly co-author another codeproject article.
|
|
|
|

|
Hello everyone, I can not do with json decode, please help?
string jsonText = "[[{\"language\":\"es\",\"isReliable\":false,\"confidence\":0.4517133956386293},{\"language\":\"pt\",\"isReliable\":false,\"confidence\":0.08356545961002786}],[{\"language\":\"en\",\"isReliable\":false,\"confidence\":0.17017828200972449},{\"language\":\"vi\",\"isReliable\":false,\"confidence\":0.13673655423883319}]]}}";
var newobj = fastJSON.JSON.Instance.ToObject<List<SubFolder>>(jsonText);
private class SubFolder
{
public string language { get; set; }
public string isReliable { get; set; }
public string confidence { get; set; }
}
> the array would be this:
{"data":{"detections":[[{"language":"es","isReliable":false,"confidence" .4517133956386293},{"language":"pt","isReliable":false,"confidence" .08356545961002786}],[{"language":"en","isReliable":false,"confidence" .17017828200972449},{"language":"vi","isReliable":false,"confidence" .13673655423883319}]]}}
|
|
|
|

|
First a List<SubFolder> becomes [{"language":null,"isReliable":null,"confidence":null},{"language":null,"isReliable":null,"confidence":null}]
Note the single square bracket, so your json string is not correct.
Second the types must be public and not private for fastJSON to work.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Im happy with this lib first of all,
but I recently found a bug
you can cast an int[][] from the json string "[[0,0,2]]"
Greetings
|
|
|
|

|
Do you have some sample code for this?
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
this will throw an exception that it can't cast an int[][] from an int[]
fastJSON.JSON.Instance.ToObject<int[][]>>("[[0,0,2]]");
|
|
|
|

|
Hello, Just wanted to ask, if there are any plans of making fastJSON available to WinRT platform? Regards, Dovydas
|
|
|
|

|
Thanks Dovydas!
Currently I don't have the resources for WinRT testing, judging by the Silverlight debacle WinRT will probably not work, I don't know.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Change fastJSON version from 1.9.6 to current version 2.0.13 and now I cannot longer deserialize classes with enum properties. Always get exception "InvalidCastException: Cannot convert type System.Int64 to type System.String".
Compiling fastJSON for .NET 2.0
Any ideas?
Best regards,
Markus
|
|
|
|

|
Thanks Mark!
Can you give me a sample code that you have problems with?
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
If jp.UseOptimizedDatasetSchema is set with false and the json text with dataset is handled by the method "Beautify", the dataset's xml schema in the json text will be not well-formed. I guess that the method of "Formatter.PrettyPrint" is not implemented correctly.
|
|
|
|

|
Modification 1: // StringBuilder replaced with string public static bool IsEscaped(string sb, int index) { bool escaped = false; while (index > 0 && sb[--index] == '\\') escaped = !escaped; return escaped; } Modification 2: case '"': case '\'': output.Append(ch); if (!IsEscaped(input, i)) // not using output // The length of output is unexpected.
|
|
|
|

|
Thanks! I will look into it and post soon.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
I had this bug too and the modifications helped.
|
|
|
|

|
A bug !!! All objects must be declared with "public", otherwise, the exception - failed to fast create instance - will be threw during deserialization from JSON text.
|
|
|
|

|
In the words of Huey Lewis : "that's the way it is".
Generally serializers work with public classes, properties and fields, and fastJSON is no exception.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
I am having problems using fastJSON in my Silverlight 4 application.
When I call: fastJSON.JSON.Instance.ToJSON(myObject);
I get an exception here:
DynamicMethod getter = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type);
Exception:
Attempt by security transparent method 'fastJSON.Reflection.CreateGetMethod(System.Type, System.Reflection.PropertyInfo)' to access security critical method 'System.Reflection.Emit.DynamicMethod..ctor(System.String, System.Type, System.Type[], System.Type)' failed.
Do you have any idea how I could get this fixed?
I took the fastJSON-SL project and builded. Then added the .dll to my SL app.
Best Regards
Frederik
|
|
|
|

|
Right click on your application and make sure the Silverlight version is 4 and not 5.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Can you compare performance with
|
|
|
|

|
Last time I checked they were not polymorphic.
(SerivceStack is compared in the article).
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
I was reviewing some of the code and noticed that you target .NET 4.0 but are using monitor locks for dictionary access. This isn't altogether terrible, but figured you might like to know MS has added concurrency oriented collections and ConcurrentDictionary<K,V> might be to your liking. It also better encapsulates common atomic operations like GetOrAdd and AddOrUpdate. Besides that, the main reason I'm avoiding this for now is the lack of Stream support. I have network and file stream data that would be inconvenient to load into memory entirely (some rather large data at times). Since you're using a forward moving tokenizer I don't think this would be entirely too difficult using a TextReader. Thanks for the article and code
|
|
|
|

|
Thanks!
In my tests (a while back) the concurrent dictionary was slower than the one in fastJSON.
TextReader and streams were slow also.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
How does fastJson handle circular references? Json.Net has a setting that limits circular references whereas the .NET Json serializer throws an error but I did not see any mention in your article about handling circular references with fastJson.
If you could comment, I would appreciate it.
|
|
|
|

|
fastJSON will go 10 levels deep into any object structure, (this is a configuration parameter).
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Thank you for the information. Where can this be configured so I can only have it go 1 or 2 deep?
|
|
|
|

|
internal sealed class JSONSerializer
{
private StringBuilder _output = new StringBuilder();
private StringBuilder _before = new StringBuilder();
readonly int _MAX_DEPTH = 10;
...
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|
|

|
Thanks!
I will fix this.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Fixed in v2.0.13
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Line 328 in JsonSerializer.cs
maybe int c = g.Count; is int c = g.Count + 1; ?
test data is (type Test is struct):
Test data = new Test(1,"Test","Test");
serialized string is (but incorrect):
{"Id":1,"Name":"Test""Picture":"Test"}
after modifying source code:
{"Id":1,"Name":"Test","Picture":"Test"}
|
|
|
|

|
I have the same bug.
@QK5762: Your fix works for me as well.
|
|
|
|

|
Ah these commas are killing me!
This patch fails in CommaTests().
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Fixed in v2.0.13
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Ah these commas are killing me!
This patch fails in CommaTests().
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Fixed in v2.0.13
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
Consecutive NULL parameters in class, with SerializeNullValues = false produces multiple commas.
eg.
var objTest = new
{
A = "foo",
B = (object)null,
C = (object)null,
D = "bar"
};
jsonInstance = JSON.Instance;
jsonInstance.Parameters = new JSONParameters
{
EnableAnonymousTypes = false,
IgnoreCaseOnDeserialize = false,
SerializeNullValues = false,
ShowReadOnlyProperties = true,
UseExtensions = false,
UseFastGuid = true,
UseOptimizedDatasetSchema = true,
UseUTCDateTime = false,
UsingGlobalTypes = false,
UseEscapedUnicode = false
};
var json = JsonFormatter.JSON.ToJSON(objTest);
json = {"A":"foo",,"D":"bar"}
|
|
|
|

|
Thanks Mabakay!
I will look into it and post a fix soon.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|

|
look at the code:
Version oVer = new Version("2012.12.12");
string sNewSer41 = fastJSON.JSON.Instance.ToJSON(oVer);
Version oTest41 = fastJSON.JSON.Instance.ToObject<Version>(sNewSer41);
sNewSer41 parameter is right.
but when deserialize it to Version instance,i find it can't success to deserialize.
Last One Code
|
|
|
|

|
I haven't tested that, if you need it you can write your own custom serializer/deserializer for that type, see the article.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Smallest, fastest polymorphic JSON serializer (with Silverlight4 and MonoDroid support)
| Type | Article |
| Licence | CPOL |
| First Posted | 19 Feb 2011 |
| Views | 1,552,593 |
| Downloads | 25,725 |
| Bookmarked | 462 times |
|
|