Skip to content
Browse files

Modified the to_json filter to force the use of the encoding paramete…

…r, as suggested by Andreas Stenius
  • Loading branch information...
1 parent ea89dd8 commit fc99ce2d1c566b9de78e13e66ebcb0167b772b29 @fcardinaux committed Feb 22, 2012
Showing with 18 additions and 30 deletions.
  1. +18 −30 modules/mod_base/filters/filter_to_json.erl
View
48 modules/mod_base/filters/filter_to_json.erl
@@ -16,48 +16,36 @@
%% See the License for the specific language governing permissions and
%% limitations under the License.
-%% Modified by François Cardinaux in order to convert UTF8 strings correctly
+%% Modified by François Cardinaux in order to convert UTF8 strings correctly.
%% Usage:
%% * If the input value contains strings of UTF-8-encoded characters:
-%% {{ value|to_json }}
-%% * If the input value contains strings of ASCII or ISO 8859-1 (= Latin-1) characters:
+%% {{ value|to_json:"utf-8" }}
+%% * If the input value contains strings of ISO 8859-1 (= Latin-1) characters:
%% {{ value|to_json:"latin-1" }}
-%% * If the input value contains strings of unicode characters, as defined in the mochijson module:
-%% {{ value|to_json:"unicode" }}
%% Note that these variants only concern the strings in the input term.
%% The output will always contain utf-8-encoded strings.
-module(filter_to_json).
--export([to_json/2, to_json/3]).
+-export([to_json/3]).
-%% @doc Convert an Erlang list or tuple to JSON.
-%% This function assumes that the input contains only strings that are composed of utf-8 characters.
-%% For other encodings, use to_json/3 instead.
-%% @spec to_json(ErlangTerm, Context) -> Json
-%% Where:
-%% * ErlangTerm = list() | tuple()
-%% * Context = Zotonic context record
-%% * Json = JSON content
-to_json(Value, _Context) ->
- Encoder = mochijson:encoder([{input_encoding, utf8}]),
- Encoder(z_convert:to_json(Value)).
-
%% @doc Convert an Erlang list or tuple to JSON
+%% This function assumes that the all strings of the input term have the same
+%% character encoding. This encoding may be UTF-8 or ISO 8859-1 (also called Latin-1).
%% @spec to_json(ErlangTerm, Encoding, Context) -> Json
%% Where:
-%% * ErlangTerm, Context and Json: like in to_json/2
-%% * Encoding: the character encoding of the strings in the input term (the output always contains utf-8 character strings)
-%% The possible values are:
-%% * "latin-1": ErlangTerm contains strings of ISO 8859-1 characters (this character set encompasses ASCII characters)
-%% * "unicode": ErlangTerm contains unicode strings, as defined in the mochijson module
-%% For utf-8 input strings, use to_json/2 instead.
+%% * ErlangTerm = list() | tuple()
+%% * Encoding = the character encoding of the strings in the input term.
+%% Following values are accepted:
+%% * "utf-8": ErlangTerm contains strings of UTF-8-encoded characters
+%% * "latin-1": ErlangTerm contains strings of ISO 8859-1 characters
+%% Note that this parameter concerns the strings in the input term only.
+%% The JSON output will always contain utf-8-encoded strings.
+%% * Context = Zotonic context record
+%% * Json = the JSON content
to_json(Value, "latin-1", _Context) ->
mochijson:encode(z_convert:to_json(Value));
-to_json(Value, "unicode", _Context) ->
- Encoder = mochijson:encoder([{input_encoding, unicode}]),
- Encoder(z_convert:to_json(Value));
-
-to_json(Value, _Encoding, Context) ->
- to_json(Value, Context).
+to_json(Value, "utf-8", _Context) ->
+ Encoder = mochijson:encoder([{input_encoding, utf8}]),
+ Encoder(z_convert:to_json(Value)).

0 comments on commit fc99ce2

Please sign in to comment.
Something went wrong with that request. Please try again.