You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2013/12/05 01:56:44 UTC

[08/28] git commit: CB-5525 WP8. Contacts Api fails in case of there is special character in contact field

CB-5525 WP8. Contacts Api fails in case of there is special character in contact field


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/85c40309
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/85c40309
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/85c40309

Branch: refs/heads/master
Commit: 85c40309a22911db7f7f33ae1472ed6444e6ebdb
Parents: f815ff9
Author: sgrebnov <se...@gmail.com>
Authored: Mon Dec 2 18:01:01 2013 +0400
Committer: sgrebnov <se...@gmail.com>
Committed: Mon Dec 2 18:01:01 2013 +0400

----------------------------------------------------------------------
 src/wp/Contacts.cs | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/85c40309/src/wp/Contacts.cs
----------------------------------------------------------------------
diff --git a/src/wp/Contacts.cs b/src/wp/Contacts.cs
index d68b0b1..8d35011 100644
--- a/src/wp/Contacts.cs
+++ b/src/wp/Contacts.cs
@@ -549,7 +549,7 @@ namespace WPCordovaClassLib.Cordova.Commands
             {
                 string contactField = string.Format(contactFieldFormat,
                                                     address.Kind.ToString(),
-                                                    address.EmailAddress);
+                                                    EscapeJson(address.EmailAddress));
 
                 retVal += "{" + contactField + "},";
             }
@@ -568,18 +568,18 @@ namespace WPCordovaClassLib.Cordova.Commands
                           "\"postalCode\":\"{6}\"," +
                           "\"country\":\"{7}\"";
 
-            string formattedAddress = address.PhysicalAddress.AddressLine1 + " "
+            string formattedAddress = EscapeJson(address.PhysicalAddress.AddressLine1 + " "
                                     + address.PhysicalAddress.AddressLine2 + " "
                                     + address.PhysicalAddress.City + " "
                                     + address.PhysicalAddress.StateProvince + " "
                                     + address.PhysicalAddress.CountryRegion + " "
-                                    + address.PhysicalAddress.PostalCode;
+                                    + address.PhysicalAddress.PostalCode);
 
             string jsonAddress = string.Format(addressFormatString,
                                                isPrefered ? "\"true\"" : "\"false\"",
                                                address.Kind.ToString(),
                                                formattedAddress,
-                                               address.PhysicalAddress.AddressLine1 + " " + address.PhysicalAddress.AddressLine2,
+                                               EscapeJson(address.PhysicalAddress.AddressLine1 + " " + address.PhysicalAddress.AddressLine2),
                                                address.PhysicalAddress.City,
                                                address.PhysicalAddress.StateProvince,
                                                address.PhysicalAddress.PostalCode,
@@ -607,7 +607,7 @@ namespace WPCordovaClassLib.Cordova.Commands
             string retVal = "";
             foreach (string website in con.Websites)
             {
-                retVal += "\"" + website + "\",";
+                retVal += "\"" + EscapeJson(website) + "\",";
             }
             return retVal.TrimEnd(',');
         }
@@ -633,12 +633,12 @@ namespace WPCordovaClassLib.Cordova.Commands
             if (con.CompleteName != null)
             {
                 retVal = string.Format(formatStr,
-                                   con.CompleteName.FirstName + " " + con.CompleteName.LastName, // TODO: does this need suffix? middlename?
-                                   con.CompleteName.LastName,
-                                   con.CompleteName.FirstName,
-                                   con.CompleteName.MiddleName,
-                                   con.CompleteName.Title,
-                                   con.CompleteName.Suffix);
+                                   EscapeJson(con.CompleteName.FirstName + " " + con.CompleteName.LastName), // TODO: does this need suffix? middlename?
+                                   EscapeJson(con.CompleteName.LastName),
+                                   EscapeJson(con.CompleteName.FirstName),
+                                   EscapeJson(con.CompleteName.MiddleName),
+                                   EscapeJson(con.CompleteName.Title),
+                                   EscapeJson(con.CompleteName.Suffix));
             }
             else
             {
@@ -665,19 +665,29 @@ namespace WPCordovaClassLib.Cordova.Commands
 
             string jsonContact = String.Format(contactFormatStr,
                                                con.GetHashCode(),
-                                               con.DisplayName,
-                                               con.CompleteName != null ? con.CompleteName.Nickname : "",
+                                               EscapeJson(con.DisplayName),
+                                               EscapeJson(con.CompleteName != null ? con.CompleteName.Nickname : ""),
                                                FormatJSONPhoneNumbers(con),
                                                FormatJSONEmails(con),
                                                FormatJSONAddresses(con),
                                                FormatJSONWebsites(con),
                                                FormatJSONName(con),
-                                               con.Notes.FirstOrDefault(),
-                                               con.Birthdays.FirstOrDefault());
+                                               EscapeJson(con.Notes.FirstOrDefault()),
+                                               EscapeJson(Convert.ToString(con.Birthdays.FirstOrDefault())));
 
             //Debug.WriteLine("jsonContact = " + jsonContact);
             // JSON requires new line characters be escaped
-            return "{" + jsonContact.Replace("\n", "\\n") + "}";
+            return "{" + jsonContact + "}";
+        }
+
+        private static string EscapeJson(string str)
+        {
+            if (String.IsNullOrEmpty(str))
+            {
+                return str;
+            }
+
+            return str.Replace("\n", "\\n").Replace("\r", "\\r").Replace("\t", "\\t").Replace("\"", "\\\"").Replace("&", "\\&");
         }
     }
 }