// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. #include "WebBrowserPrivatePCH.h" #include "WebJSStructSerializerBackend.h" #if WITH_CEF3 /* Private methods *****************************************************************************/ void FWebJSStructSerializerBackend::AddNull(UProperty* Property) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetNull(*Property->GetName()); break; case StackItem::STYPE_LIST: Current.ListValue->SetNull(Current.ListValue->GetSize()); break; } } void FWebJSStructSerializerBackend::Add(UProperty* Property, bool Value) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetBool(*Property->GetName(), Value); break; case StackItem::STYPE_LIST: Current.ListValue->SetBool(Current.ListValue->GetSize(), Value); break; } } void FWebJSStructSerializerBackend::Add(UProperty* Property, int32 Value) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetInt(*Property->GetName(), Value); break; case StackItem::STYPE_LIST: Current.ListValue->SetInt(Current.ListValue->GetSize(), Value); break; } } void FWebJSStructSerializerBackend::Add(UProperty* Property, double Value) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetDouble(*Property->GetName(), Value); break; case StackItem::STYPE_LIST: Current.ListValue->SetDouble(Current.ListValue->GetSize(), Value); break; } } void FWebJSStructSerializerBackend::Add(UProperty* Property, FString Value) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetString(*Property->GetName(), *Value); break; case StackItem::STYPE_LIST: Current.ListValue->SetString(Current.ListValue->GetSize(), *Value); break; } } void FWebJSStructSerializerBackend::Add(UProperty* Property, UObject* Value) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetDictionary(*Property->GetName(), Scripting->ConvertObject(Value)); break; case StackItem::STYPE_LIST: Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Scripting->ConvertObject(Value)); break; } } /* IStructSerializerBackend interface *****************************************************************************/ void FWebJSStructSerializerBackend::BeginArray(const FStructSerializerState& State) { CefRefPtr ListValue = CefListValue::Create(); Stack.Push(StackItem(State.ValueProperty->GetName(), ListValue)); } void FWebJSStructSerializerBackend::BeginStructure(const FStructSerializerState& State) { if (State.ValueProperty != nullptr) { CefRefPtr DictionaryValue = CefDictionaryValue::Create(); Stack.Push(StackItem(State.ValueProperty->GetName(), DictionaryValue)); } else { Result = CefDictionaryValue::Create(); Stack.Push(StackItem(FString(), Result)); } } void FWebJSStructSerializerBackend::EndArray(const FStructSerializerState& /*State*/) { StackItem Previous = Stack.Pop(); check(Previous.Kind == StackItem::STYPE_LIST); check(Stack.Num() > 0); // The root level object is always a struct StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetList(*Previous.Name, Previous.ListValue); break; case StackItem::STYPE_LIST: Current.ListValue->SetList(Current.ListValue->GetSize(), Previous.ListValue); break; } } void FWebJSStructSerializerBackend::EndStructure(const FStructSerializerState& /*State*/) { StackItem Previous = Stack.Pop(); check(Previous.Kind == StackItem::STYPE_DICTIONARY); if (Stack.Num() > 0) { StackItem& Current = Stack.Top(); switch (Current.Kind) { case StackItem::STYPE_DICTIONARY: Current.DictionaryValue->SetDictionary(*Previous.Name, Previous.DictionaryValue); break; case StackItem::STYPE_LIST: Current.ListValue->SetDictionary(Current.ListValue->GetSize(), Previous.DictionaryValue); break; } } else { check(Result == Previous.DictionaryValue); } } void FWebJSStructSerializerBackend::WriteComment(const FString& Comment) { // Cef values do not support comments } void FWebJSStructSerializerBackend::WriteProperty(const FStructSerializerState& State, int32 ArrayIndex) { // booleans if (State.ValueType == UBoolProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } // unsigned bytes & enumerations else if (State.ValueType == UByteProperty::StaticClass()) { UByteProperty* ByteProperty = Cast(State.ValueProperty); if (ByteProperty->IsEnum()) { Add(State.ValueProperty, ByteProperty->Enum->GetEnumName(ByteProperty->GetPropertyValue_InContainer(State.ValueData, ArrayIndex))); } else { Add(State.ValueProperty, (double)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } } // floating point numbers else if (State.ValueType == UDoubleProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UFloatProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } // signed integers else if (State.ValueType == UIntProperty::StaticClass()) { Add(State.ValueProperty, (int32)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UInt8Property::StaticClass()) { Add(State.ValueProperty, (int32)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UInt16Property::StaticClass()) { Add(State.ValueProperty, (int32)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UInt64Property::StaticClass()) { Add(State.ValueProperty, (double)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } // unsigned integers else if (State.ValueType == UUInt16Property::StaticClass()) { Add(State.ValueProperty, (int32)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UUInt32Property::StaticClass()) { Add(State.ValueProperty, (double)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UUInt64Property::StaticClass()) { Add(State.ValueProperty, (double)Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } // names & strings else if (State.ValueType == UNameProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString()); } else if (State.ValueType == UStrProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } else if (State.ValueType == UTextProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex).ToString()); } // classes & objects else if (State.ValueType == UClassProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)->GetPathName()); } else if (State.ValueType == UObjectProperty::StaticClass()) { Add(State.ValueProperty, Cast(State.ValueProperty)->GetPropertyValue_InContainer(State.ValueData, ArrayIndex)); } // unsupported property type else { GLog->Logf(ELogVerbosity::Warning, TEXT("FWebJSStructSerializerBackend: Property %s cannot be serialized, because its type (%s) is not supported"), *State.ValueProperty->GetName(), *State.ValueType->GetName()); } } #endif