Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Poison to Jason #2237

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config :plug, :types, %{
}

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Poison
config :phoenix, :json_library, Jason

# General application configuration
config :ask,
Expand Down
1 change: 1 addition & 0 deletions lib/ask/ecto_types/day_of_week.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Ask.DayOfWeek do
alias __MODULE__

@derive Jason.Encoder
defstruct [:sun, :mon, :tue, :wed, :thu, :fri, :sat]

def cast(%DayOfWeek{} = day_of_week) do
Expand Down
4 changes: 2 additions & 2 deletions lib/ask/ecto_types/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule Ask.Ecto.Type.JSON do
use Ecto.Type
def type, do: :longtext
def cast(any), do: {:ok, any}
def load(string) when is_binary(string), do: Poison.decode(string)
def dump(json), do: Poison.encode(json)
def load(string) when is_binary(string), do: Jason.decode(string)
def dump(json), do: Jason.encode(json)
end
5 changes: 3 additions & 2 deletions lib/ask/ecto_types/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Ask.Schedule do
alias __MODULE__
alias Ask.{DayOfWeek, ScheduleError, SystemTime}

@derive Jason.Encoder
defstruct [
:day_of_week,
:start_time,
Expand Down Expand Up @@ -96,7 +97,7 @@ defmodule Ask.Schedule do

defp cast_blocked_days(_), do: []

def load(string) when is_binary(string), do: cast(Poison.decode!(string))
def load(string) when is_binary(string), do: cast(Jason.decode!(string))
def load(nil), do: {:ok, always()}
def load(_), do: :error

Expand All @@ -110,7 +111,7 @@ defmodule Ask.Schedule do
def dump(%Schedule{day_of_week: day_of_week} = schedule) do
{:ok, day_of_week} = DayOfWeek.dump(day_of_week)
schedule = %{schedule | day_of_week: day_of_week, blocked_days: schedule.blocked_days || []}
Poison.encode(schedule)
Jason.encode(schedule)
end

def dump(_), do: :error
Expand Down
5 changes: 3 additions & 2 deletions lib/ask/ecto_types/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Ask.Stats do
use Ecto.Type
alias __MODULE__

@derive Jason.Encoder
defstruct total_received_sms: 0,
total_sent_sms: 0,
total_call_time: nil,
Expand Down Expand Up @@ -52,10 +53,10 @@ defmodule Ask.Stats do
def cast(nil), do: {:ok, %Stats{}}
def cast(_), do: :error

def load(string) when is_binary(string), do: cast(Poison.decode!(string))
def load(string) when is_binary(string), do: cast(Jason.decode!(string))
def load(_), do: :error

def dump(%Stats{} = stats), do: Poison.encode(stats)
def dump(%Stats{} = stats), do: Jason.encode(stats)
def dump(_), do: :error

def total_received_sms(%Stats{total_received_sms: count}), do: count
Expand Down
4 changes: 2 additions & 2 deletions lib/ask/ecto_types/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ defmodule Ask.Ecto.Type.Steps do
end

def load(string) when is_binary(string) do
case Poison.decode(string) do
case Jason.decode(string) do
{:ok, steps} -> {:ok, transform(steps)}
any -> any
end
end

def dump(json) do
Poison.encode(json)
Jason.encode(json)
end

defp transform(steps) when is_nil(steps), do: nil
Expand Down
4 changes: 2 additions & 2 deletions lib/ask/floip_pusher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ defmodule Ask.FloipPusher do
end

def create_package(survey, endpoint, responses_uri) do
{:ok, body} = FloipPackage.descriptor(survey, responses_uri) |> Poison.encode()
{:ok, body} = FloipPackage.descriptor(survey, responses_uri) |> Jason.encode()

endpoint_uri = String.to_charlist("#{endpoint.uri}/flow-results/packages")

Expand All @@ -150,7 +150,7 @@ defmodule Ask.FloipPusher do
end

defp push_responses(endpoint, responses) do
{:ok, body} = Poison.encode(responses)
{:ok, body} = Jason.encode(responses)

endpoint_uri =
String.to_charlist(
Expand Down
2 changes: 1 addition & 1 deletion lib/ask/json_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Ask.JsonSchema do

defp load_schema(schema_path) do
File.read!(schema_path)
|> Poison.decode!()
|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ask/metrics/respondent_stats_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Ask.RespondentStatsMetrics do
end

defp parse_mode(key, value) when key == :mode and value != "" do
{key, Poison.decode!(value) |> Enum.join(",")}
{key, Jason.decode!(value) |> Enum.join(",")}
end

defp parse_mode(key, value), do: {key, value}
Expand Down
2 changes: 1 addition & 1 deletion lib/ask/number_translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Ask.NumberTranslator.Macros do
|> Enum.map(fn filename ->
filename = filename |> Path.basename(".json")

data = Poison.decode!(File.read!("#{__DIR__}/numbers/#{filename}.json"))
data = Jason.decode!(File.read!("#{__DIR__}/numbers/#{filename}.json"))
data = {:%{}, [], data |> Enum.to_list()}

quote do
Expand Down
2 changes: 1 addition & 1 deletion lib/ask/o_auth_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ defmodule Ask.OAuthToken do
end

def access_token(token) do
Poison.Decode.decode(token.access_token, as: %OAuth2.AccessToken{})
OAuth2.AccessToken.new(token.access_token["access_token"])
end
end
2 changes: 1 addition & 1 deletion lib/ask/runtime/questionnaire_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Ask.Runtime.QuestionnaireExport do
manifest: manifest,
audio_entries: audio_entries
}) do
{:ok, json} = Poison.encode(manifest)
{:ok, json} = Jason.encode(manifest)

json_entry =
Stream.map([json], fn json ->
Expand Down
1 change: 1 addition & 0 deletions lib/ask/runtime/reply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Ask.Runtime.Reply do
end

defmodule Ask.Runtime.ReplyStep do
@derive Jason.Encoder
defstruct prompts: [],
id: nil,
title: nil,
Expand Down
8 changes: 5 additions & 3 deletions lib/ask/runtime/respondent_group_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ defmodule Ask.Runtime.RespondentGroupAction do
zip =
canonical_numbers
|> Enum.zip(phone_numbers)
|> List.foldl(%{}, fn ({k, v}, zip) -> Map.put(zip, k, v) end)
|> List.foldl(%{}, fn {k, v}, zip -> Map.put(zip, k, v) end)

existing_numbers
|> List.foldl(zip, fn (n, zip) -> Map.delete(zip, n) end) # remove duplicates (using canonical number)
|> Map.values() # return the de-duplicated list of phone numbers
# remove duplicates (using canonical number)
|> List.foldl(zip, fn n, zip -> Map.delete(zip, n) end)
# return the de-duplicated list of phone numbers
|> Map.values()
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ask/url_shortener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Ask.UrlShortener do
end

def build_short_url(host, response_body) do
"#{host}/#{Poison.decode!(response_body)["key"]}"
"#{host}/#{Jason.decode!(response_body)["key"]}"
end

defp get_api_key, do: ConfigHelper.get_config(__MODULE__, :url_shortener_api_key)
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/controllers/folder_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule AskWeb.FolderController do
from(s in Survey,
left_join: ps in PanelSurvey,
on: s.panel_survey_id == ps.id,
where: (ps.folder_id == ^folder_id or s.folder_id == ^folder_id),
where: ps.folder_id == ^folder_id or s.folder_id == ^folder_id,
where: s.state == ^:running,
select: count(s.id)
)
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/controllers/questionnaire_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ defmodule AskWeb.QuestionnaireController do
|> Enum.into(%{})

json = files |> Map.get("manifest.json")
{:ok, manifest} = Poison.decode(json)
{:ok, manifest} = Jason.decode(json)

audio_files = manifest |> Map.get("audio_files")

Expand Down
6 changes: 3 additions & 3 deletions lib/ask_web/controllers/respondent_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ defmodule AskWeb.RespondentController do
|> Enum.map(fn {state, disposition, questionnaire_id, mode, count} ->
reference_id =
if mode && questionnaire_id do
"#{questionnaire_id}#{mode |> Poison.decode!() |> Enum.join("")}"
"#{questionnaire_id}#{mode |> Jason.decode!() |> Enum.join("")}"
else
nil
end
Expand Down Expand Up @@ -652,7 +652,7 @@ defmodule AskWeb.RespondentController do
{r.questionnaire_id, r.mode, r.date, fragment("CAST(? AS UNSIGNED)", sum(r.count))}
)
|> Enum.map(fn {questionnaire_id, mode, completed_at, count} ->
{"#{questionnaire_id}#{mode |> Poison.decode!() |> Enum.join("")}", completed_at, count}
{"#{questionnaire_id}#{mode |> Jason.decode!() |> Enum.join("")}", completed_at, count}
end)
end

Expand All @@ -665,7 +665,7 @@ defmodule AskWeb.RespondentController do
select: {r.mode, r.date, fragment("CAST(? AS UNSIGNED)", sum(r.count))}
)
|> Enum.map(fn {mode, completed_at, count} ->
{mode |> Poison.decode!() |> Enum.join(""), completed_at, count}
{mode |> Jason.decode!() |> Enum.join(""), completed_at, count}
end)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule AskWeb.Endpoint do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison,
json_decoder: Jason,
length: 1_073_741_824,
read_timeout: 60_000

Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/templates/mobile_survey/index.html.eex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<body>
<div id="root" role="main" data-config="<%= Poison.encode!(%{introMessage: @mobile_web_intro_message, colorStyle: @color_style}) %>"></div>
<div id="root" role="main" data-config="<%= Jason.encode!(%{introMessage: @mobile_web_intro_message, colorStyle: @color_style}) %>"></div>
<script>
window.mixEnv = "<%= Mix.env %>"
window.respondentId = <%= @respondent_id %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<body>
<div id="root" role="main" data-config="<%= Poison.encode!(%{introMessage: @mobile_web_intro_message, colorStyle: @color_style}) %>"></div>
<div id="root" role="main" data-config="<%= Jason.encode!(%{introMessage: @mobile_web_intro_message, colorStyle: @color_style}) %>"></div>
<script>
window.mixEnv = "<%= Mix.env %>"
window.respondentId = "<%= @respondent_id %>"
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/views/layout_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule AskWeb.LayoutView do
intercom_app_id: Ask.Intercom.intercom_app_id()
}

{:ok, config_json} = client_config |> Poison.encode()
{:ok, config_json} = client_config |> Jason.encode()
config_json
end

Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ defmodule Ask.Mixfile do
{:zstream, "~> 0.2.0"},
# required by Phoenix
{:jason, "~> 1.0"},
# until we migrate Surveda to Jason
# OAuth2 v0.7.0 require Poison for tests (https://hexdocs.pm/oauth2/changelog.html#improvements-6)
{:poison, "~> 3.1"},

# {:telemetry, "~> 0.4.3"},

# held back because of warnings & errors with newer versions
{:swoosh, "~> 0.17.0"},
{:swoosh, "~> 0.17.0"}
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ defmodule Ask.Repo.Migrations.MultilingualQuestionnaires do

def upgrade(steps) do
steps
|> Poison.decode!()
|> Jason.decode!()
|> Enum.map(&upgrade_step/1)
|> Poison.encode!()
|> Jason.encode!()
end

def upgrade_step(step) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ defmodule Ask.Repo.Migrations.AddRangesToNumericSteps do

def upgrade(steps) do
steps
|> Poison.decode!()
|> Jason.decode!()
|> Enum.map(&upgrade_step/1)
|> Poison.encode!()
|> Jason.encode!()
end

def upgrade_step(step) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ defmodule Ask.Repo.Migrations.ReplaceEmptySkipLogicWithNull do

def upgrade(steps) do
steps
|> Poison.decode!()
|> Jason.decode!()
|> Enum.map(&upgrade_step/1)
|> Poison.encode!()
|> Jason.encode!()
end

def upgrade_step(step) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ defmodule Ask.Repo.Migrations.ChangeQuizChoiceResponsesLangModeToModeLang do

def update(steps, update_function) do
steps
|> Poison.decode!()
|> Jason.decode!()
|> Enum.map(update_function)
|> Poison.encode!()
|> Jason.encode!()
end

def upgrade_step(step) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Ask.Repo.Migrations.DeleteInvalidPropertiesFromSteps do
def up do
json_schema =
File.read!("#{Application.app_dir(:ask)}/priv/schema.json")
|> Poison.decode!()
|> Jason.decode!()
|> Schema.resolve()

Questionnaire
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ defmodule Ask.Repo.Migrations.MigrateSurveySchedule do
def cast(nil), do: {:ok, %Schedule{}}
def cast(_), do: :error

def load(string) when is_binary(string), do: cast(Poison.decode!(string))
def load(string) when is_binary(string), do: cast(Jason.decode!(string))
def load(nil), do: {:ok, %Schedule{}}
def load(_), do: :error

def dump(%Schedule{day_of_week: day_of_week} = schedule) do
{:ok, day_of_week} = NewDayOfWeek.dump(day_of_week)
schedule = %{schedule | day_of_week: day_of_week, blocked_days: schedule.blocked_days || []}
Poison.encode(schedule)
Jason.encode(schedule)
end

def dump(_), do: :error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ defmodule Ask.Repo.Migrations.RenamePartialDispositionToInterimPartial do

def update(steps, update_function) do
steps
|> Poison.decode!()
|> Jason.decode!()
|> Enum.map(update_function)
|> Poison.encode!()
|> Jason.encode!()
end

def upgrade_step(step) do
Expand Down
Loading