[CLOSED] JSON.Deserialize backslash and double-quote fixing

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] JSON.Deserialize backslash and double-quote fixing

    hi,

    still working in serach inside grids. now if filter value entered by user contain '\' or ' " ' the JSON.Deserialize throw bug. i already was able to fix double-quote by using regeX before passing the json string to the webservice:

    filterValue.replace(/"/g, "\\\"");
    but how i can handle both '\' or ' " ' in one replace statement or do you know better method?
    Last edited by Daniil; Jun 27, 2011 at 12:51 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Maybe
    Ext.util.Format.htmlEncode(s);
    and decode at server side.
  3. #3
    i don't think its cover all cases:

    htmlEncode : function(value) {
                return !value ? value : String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
            },
    the backslash not covered in html encoding.
  4. #4
    Yes, you are right, there is no backslash, I forgot.

    Well, you was on the right direction - I mean manually replacing backslash and double quote.
  5. #5
    i saw function called encodeString in Ext.util.JSON is that gonna solve the problem! but in server side no decode? or maybe its not required to decode for the JSON.Deserialize!
  6. #6
    Ok, lets narrow to the problem.

    How can I reproduce the initial problem?

    I try to enter backslash and double quote, but all appears to be working fine here in the example below.

    Example

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        [DirectMethod]
        public void Test(string value)
        {
            X.Msg.Alert("DirectMethod", JSON.Deserialize<object>(value).ToString()).Show();
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" DirectMethodNamespace="X" />
            <ext:TextField ID="TextField1" runat="server" />
            <ext:Button runat="server" Text="Test">
                <Listeners>
                    <Click Handler="X.Test({ value : TextField1.getValue() });" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
  7. #7
    that's for sure its easy since you are using 'object' type! in my case the dal receive the filters "where params" as listDictionary
    JSON.Deserialize<ListDictionary>(filters)
    also i pass the filters string to the webservice in json format. i'm not sure if the call to directMethod auto convert the value to json in your example! if you did these 2 changes then you should see the bug in Deserialize().

    i fix it somehow by doing this for every filter box:
    var fldValue = txtFilterEl.component.getValue() + "";
    fldValue=fldValue.replace(/\\/g, "\\\\");/*this should come first*/
    fldValue=fldValue.replace(/"/g, "\\\"");
    for '"' it fine but for '\' if the text contain it, it will not appear until i put 2 backslash not one!
  8. #8
    Quote Originally Posted by webclouder View Post
    i'm not sure if the call to directMethod auto convert the value to json in your example! if you did these 2 changes.
    I'm sure:) DirectMethods parameters are encoded automatically.

    Please show the problem json. You can use:
    X.Msg.Alert("filters", filters).Show();
    and just post here the message.
  9. #9
    the first one in case of '"', second is '\'

    {"Message":"After parsing a value an unexpected character was encountered: \". Line 1, position 41.","StackTrace":" at Newtonsoft.Json.JsonTextReader.ParsePostValue(Char currentChar)\r\n at Newtonsoft.Json.JsonTextReader.Read()\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Ext.Net.JSON.Deserialize(String value, Type type, List`1 converters, IContractResolver resolver)\r\n at Ext.Net.JSON.Deserialize[T](String value)\r\n at WServices.GenericService.LoadGridData(Int32 start, Int32 limit, String dbTableName, String dataColumns, Int32 pageSize, String filters) in E:\\work\\Prj\\Services\\GenericService.asmx.cs:line 467","ExceptionType":"Newtonsoft.Json.JsonReaderException"}
    
    *******************
    
    {"Message":"After parsing a value an unexpected character was encountered: u. Line 1, position 44.","StackTrace":" at Newtonsoft.Json.JsonTextReader.ParsePostValue(Char currentChar)\r\n at Newtonsoft.Json.JsonTextReader.Read()\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, String id)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Ext.Net.JSON.Deserialize(String value, Type type, List`1 converters, IContractResolver resolver)\r\n at Ext.Net.JSON.Deserialize[T](String value)\r\n at Services.GenericService.LoadGridData(Int32 start, Int32 limit, String dbTableName, String dataColumns, Int32 pageSize, String filters) in E:\\work\\Prj\\Services\\GenericService.asmx.cs:line 467","ExceptionType":"Newtonsoft.Json.JsonReaderException"}
  10. #10
    Well, I see the exception trace.

    I need the json string which comes from client side.
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] json deserialize problem
    By albayrak in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Jan 21, 2015, 2:28 PM
  2. Replies: 2
    Last Post: May 29, 2011, 3:42 AM
  3. [CLOSED] Json Deserialize error when save
    By vali1993 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 07, 2010, 2:39 PM
  4. [CLOSED] JSON.Deserialize Lastes SVN
    By egodoy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 21, 2010, 5:59 PM
  5. [CLOSED] [1.0] Ext.encode and JSON.Deserialize
    By state in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 14, 2010, 8:03 AM

Posting Permissions