1

I try to load configuration of DevExpress GridControl from Json string. When I try to run code like that:

GridColumnSummaryItem item = JsonConvert.DeserializeObject<GridColumnSummaryItem>("{DisplayFormat:'Sum: {0:n2}',FieldName:'TextNumCap',SummaryType:'Sum'}");

I will get the error:

Newtonsoft.Json.JsonSerializationException was unhandled
  HResult=-2146233088
  Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'DevExpress.XtraGrid.GridColumnSummaryItem' because the type requires a JSON string value to deserialize correctly.
To fix this error either change the JSON to a JSON string value or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '', line 1, position 2.
  Source=Newtonsoft.Json
  StackTrace:
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
       at TestWinforms.Form1..ctor() in d:\Projects\VS2013\TestWinforms\TestWinforms\Form1.cs:line 38
       at TestWinforms.Program.Main() in d:\Projects\VS2013\TestWinforms\TestWinforms\Program.cs:line 23
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

I am using C#, .Net 4.0 with VS2013, DevExpress 15.

QuangND
  • 97
  • 1
  • 12
  • Why json when DevExpress fully support Xml for their control customization? – Kinetic Aug 19 '16 at 22:00
  • Because in my software, I use Json to exchange data between client and server. – QuangND Aug 20 '16 at 03:40
  • The most likely cause for this is that `GridColumnSummaryItem` has a [custom `TypeConverter`](https://msdn.microsoft.com/en-us/library/ayybcxe5.aspx). In such cases Json.NET will use it to serialize the object as a string, see [Serialization Guide](http://www.newtonsoft.com/json/help/html/serializationguide.htm). If you don't want to use the typeconverter, see [Newtonsoft.JSON cannot convert model with TypeConverter attribute](http://stackoverflow.com/a/31328131/3744182) for instructions how to disable it. – dbc Dec 23 '16 at 22:06

0 Answers0