GIDFakeJSONSerializerImpl.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright 2025 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h"
  17. #import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  19. @implementation GIDFakeJSONSerializerImpl
  20. - (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject
  21. error:(NSError *_Nullable *_Nullable)error {
  22. _capturedJSONObject = [jsonObject copy];
  23. // Check if a serialization error should be simulated.
  24. if (self.serializationError) {
  25. if (error) {
  26. *error = self.serializationError;
  27. }
  28. return nil;
  29. }
  30. // If not failing, fall back to the real serialization path.
  31. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
  32. options:0
  33. error:error];
  34. if (!jsonData) {
  35. return nil;
  36. }
  37. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  38. }
  39. @end