Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsArrayUtils.h"
8 : #include "PaymentRequestUtils.h"
9 : #include "nsIMutableArray.h"
10 : #include "nsISupportsPrimitives.h"
11 : #include "nsIJSON.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : nsresult
17 0 : SerializeFromJSObject(JSContext* aCx, JS::HandleObject aObject, nsAString& aSerializedObject) {
18 0 : nsCOMPtr<nsIJSON> serializer = do_CreateInstance("@mozilla.org/dom/json;1");
19 0 : if (NS_WARN_IF(!serializer)) {
20 0 : return NS_ERROR_FAILURE;
21 : }
22 0 : JS::RootedValue value(aCx, JS::ObjectValue(*aObject));
23 0 : return serializer->EncodeFromJSVal(value.address(), aCx, aSerializedObject);
24 : }
25 :
26 : nsresult
27 0 : DeserializeToJSObject(const nsAString& aSerializedObject, JSContext* aCx, JS::MutableHandleObject aObject) {
28 0 : nsCOMPtr<nsIJSON> deserializer = do_CreateInstance("@mozilla.org/dom/json;1");
29 0 : if (NS_WARN_IF(!deserializer)) {
30 0 : return NS_ERROR_FAILURE;
31 : }
32 0 : JS::RootedValue value(aCx);
33 0 : JS::MutableHandleValue handleVal(&value);
34 0 : nsresult rv = deserializer->DecodeToJSVal(aSerializedObject, aCx, handleVal);
35 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
36 0 : return rv;
37 : }
38 0 : if (value.isObject()) {
39 0 : aObject.set(&value.toObject());
40 : } else {
41 0 : aObject.set(nullptr);
42 : }
43 0 : return NS_OK;
44 : }
45 :
46 : } // end of namespace dom
47 : } // end of namespace mozilla
|